最近我正在开发一个将数据导出到excel的代码。 我使用apache POI开发此功能。
请考虑以下代码。
public static void prepareDateForCSVGeneration(List<DataList> dataList,HttpServletResponse response)
throws CashBookingInquiryServiceApplicationException {
try (ServletOutputStream out = response.getOutputStream()) {
XSSFWorkbook workbook = new XSSFWorkbook();
//below method creates headers and rows
convertToExcel(workbook, dataList);
response.setContentType("text/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
workbook.write(out);
out.flush();
} catch (IOException exception) {
System.out.println("Exceptin in excel "+exception.getMessage());
exception.printStackTrace() ;
}
}
一切正常,意味着excel文件成功下载但在控制台之后它会抛出异常:
Exceptin in excel Stream is closed [err] java.io.IOException: Stream is closed [err] at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate(HttpOutputStreamImpl.java:213) [err] at [internal classes]
不确定为什么会这样。
我提到[java IO Exception: Stream Closed
但我没有找到任何具体的解决方案。 你能建议怎么做? 提前谢谢。