gpt4 book ai didi

java - 将 POI SXSSFWorkbook 流式传输到 servlet 输出流

转载 作者:行者123 更新时间:2023-12-04 17:31:09 26 4
gpt4 key购买 nike

我们正在构建一个 Spring boot REST 端点,它会生成一个大型 XLS 文件(可能包含约 100 万行)并提供下载。当前解决方案使用 Apache POI 库的 SXSSF API 来创建工作簿;之后,我们将工作簿写入输出流,将流收集到一个字节数组中,然后提供这个供下载。

当我们添加更多行时,如何流式传输工作簿的内容,这样我们就不会将整个文件保存在内存中?

当前解决方案的代码

@RequestMapping(path = "/download/xls", method = RequestMethod.GET, produces = org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<InputStreamResource> downloadXls(HttpServletResponse response, XlsRequest request) throws FileNotFoundException, InternalServerErrorException {

byte[] data = downloadIssuesAsExcel(response, request);

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Description", "File Transfer");
headers.add("Content-Disposition", "attachment; filename=justAFile.xlsx");
headers.add("Content-Transfer-Encoding", "binary");
headers.add("Connection", "Keep-Alive");
headers.setContentType(
org.springframework.http.MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
InputStreamResource isr = new InputStreamResource(new ByteArrayInputStream(data));
return ResponseEntity.ok().contentLength(data.length).headers(headers).body(isr);
}

public byte[] downloadIssuesAsExcel(HttpServletResponse response, XlsRequest request)
throws InternalServerErrorException {
try {
SXSSFWorkbook workbook = createExcel(request, response);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
workbook.write(stream);
workbook.dispose();
workbook.close();
stream.close();
return stream.toByteArray();
} catch (Exception e) {
throw new InternalServerErrorException("IO exception while downloading XLS file", e);
}
}

还尝试直接在 response.getOutputStream() 中写入工作簿内容,但文件以某种方式损坏。

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

response.setHeader("Content-Description", "File Transfer");
response.setHeader("Content-Disposition", "attachment; filename=" + issueDataService.getExcelName(request));
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Connection", "Keep-Alive");

SXSSFWorkbook workbook = createExcel(request, response);
workbook.write(response.getOutputStream());
workbook.dispose();
workbook.close();

最佳答案

我刚刚使用您的代码作为模板并创建了运行良好的 Controller

@RestController
public class XlsxController {

@RequestMapping(path = "/download/xls", method = RequestMethod.GET, produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
public void downloadXls(HttpServletResponse r) throws IOException {
r.setHeader("Content-Description", "File Transfer");
r.setHeader("Content-Disposition", "attachment; filename=justAFile.xlsx");
r.setHeader("Content-Transfer-Encoding", "binary");
r.setHeader("Connection", "Keep-Alive");

try (SXSSFWorkbook w = getWorkbook()) {
w.write(r.getOutputStream());
}
}

100万行权重超过40Mb的工作簿

org.springframework:spring-webmvc:5.2.2.RELEASE
org.apache.poi:poi-ooxml:4.1.1

关于java - 将 POI SXSSFWorkbook 流式传输到 servlet 输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com