<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
@GetMapping("/excel/{key}")
public void excel(@PathVariable("key") String keyword, HttpServletResponse response) throws IOException {
ExportExcel excel = statistic.retrieveIssue(keyword);
String fileName = keyword + ".xls";
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
try (Workbook workbook = excel.createWorkbook();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());) {
workbook.write(bufferedOutputStream);
}
}
·```java
写入本地文件时,一直抛出文档损坏;
应该使用:
target.write( new File(dir + “/xxx.xlsx”));
而不是:
target.write(new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dir + “/xxx.xlsx”))));