问题描述:

<%

response.reset();

response.setHeader("Content-Disposition", "attachment;filename=\"" + "test.xls\"");

response.setHeader("Content-Transfer-Encoding", "binary");

response.setContentType("application/vnd.ms-excel");

InputStream is = new FileInputStream(realPath);

//OutputStream outStream = response.getOutputStream();

JasperPrint jasperPrint = JasperFillManager.fillReport(is,

parameters, new JRBeanCollectionDataSource(pdfList));

JRAbstractExporter exporter = new JExcelApiExporter();

exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);

exporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);

exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);

exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, out);

exporter.exportReport();

outStream.flush();

outStream.close();

out.clear();

out =pageContext.pushBody();

%>

we use the code above to generate an excel, and it works well in tomcat + windows, but after we upload to linux + weblogic server, the excel is corrupted. I use text editor to open the excel, I found it add several empty line in the excel, which caused the excel can not be open successfully, anyone can point me the right direction ? Why there are space ? How it comes ?

Thanks in advance !

网友答案:

I suspect that your use of pageContext.pushBody() may be the culprit. pushBody is, as far as I know, meant for updating the output in the scope of a JSP tag.

When you are generating binary content such as an excel file, where you need to be absolutely sure that the intended bytes, and only the intended bytes reach the browser, you need to write those bytes, flush the output, and then make sure that nothing else gets written. By invoking pushBody(), you are making it so that more content can be written to the output, and any blank lines (and the carriage returns / line feeds between them) in the JSP page could be output.

All in all, I suggest that you just not do this in a JSP - do it in a Servlet instead.

相关阅读:
Top