【发布时间】:2019-11-26 23:33:53
【问题描述】:
我正在尝试生成一个.XLSX 文件并下载它。我使用Apache POI 生成excel 文件,我使用Swagger UI 测试端点。我一直在寻找几个小时,运气不佳。我用来生成 Excel 文件的代码是:
@GetMapping(path = "/download")
public ResponseEntity<ByteArrayResource> download(@RequestParam(name = "ids") long [] ids) {
// ... other code ...
XSSFWorkbook workbook = createWorkbook(reports);
LocalDate date = LocalDate.now();
String filename = "MYC " + date.getYear() + "." + date.getMonthValue();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
header.set(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.xlsx", filename));
workbook.write(stream);
workbook.close();
return new ResponseEntity<>(new ByteArrayResource(stream.toByteArray()), header, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
使用Swagger UI 进行测试时,我拨打电话并得到以下回报:
我在 SO 上找到的答案建议使用 force-download 而不是 vnd.openxmlformats-officedocument.spreadsheetml.sheet,所以我继续尝试。这一次,Swagger UI 实际上生成了一个Download file 链接。我可以点击它,文件会下载得很好。但是使用force-download真的是一个合适的方法吗?
现在,我尝试在我的前端下载,无论我使用的是force-download 还是vnd.openxmlformats-officedocument.spreadsheetml.sheet,我都会得到一个“损坏的”.xlsx 文件。
刚刚在写我自己的时候发现this SO问题,这个人似乎也收到了相同类型的回复。他通过将二进制数据转换为字符串来修复它。不知道这是否也是我应该做的以及我将如何去做。
【问题讨论】:
标签: java excel spring apache-poi xlsx