创建XLSX文件时出现错误。下面的方法打开一个文件,修改此文件,并将结果保存在另一个文件中。此过程有效,但始终显示错误。
这是创建和下载文件的方法:
@RequestMapping("/excelLogbee")
public ModelAndView logbee(HttpServletResponse response) {
List<Pedido> todosPedidosLogbee = cadastroPedidoService.filtrarLogbee();
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(new FileInputStream(new File("Template Importacao - Logbee.xlsx")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
XSSFSheet sheet = workbook.getSheet("Solicitação");
int rowCount = 9;
Row row = sheet.getRow(rowCount);
for (Pedido pedido : todosPedidosLogbee) {
row = sheet.getRow(++rowCount);
row.getCell(4).setCellValue(pedido.getCliente().getNome());
row.getCell(5).setCellValue(pedido.getCliente().getEndereco() + ", " + pedido.getCliente().getNumeroEndereco());
}
Calendar dataHoje = Calendar.getInstance();
String fileName = "logbee_control_breggion - " + dataHoje.get(Calendar.DAY_OF_MONTH) + "-"
+ dataHoje.get(Calendar.MONTH - 1) + "-" + dataHoje.get(Calendar.YEAR) + ".xlsx";
try (FileOutputStream outputStream = new FileOutputStream(fileName)) {
workbook.write(outputStream);
ClientOutput clientOutput = new ClientOutput(response);
DownloadHelper.download(clientOutput, "", fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new ModelAndView(PESQUISA_PEDIDO_LOGBEE_VIEW);
mv.addObject("pedidos", todosPedidosLogbee);
return mv;
}
这是错误:
2017-10-18 17:41:27.522 ERROR 9008 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].
[localhost] : Exception Processing ErrorPage[errorCode=0,
location=/error]
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-
我尝试close()
和flush()
但没有效果。