<%@ page contentType="text/html; charset=UTF-8"%> <%@page import="java.text.SimpleDateFormat"%> <%@page import="com.forgon.tools.date.DateTools"%> <%@page import="java.util.Date"%> <%@page import="java.io.File"%> <%@page import="com.forgon.tools.itextpdf.ExcelToPdf"%> <%@page import="java.io.FileInputStream"%> <%@page import="java.io.InputStream"%> <%@page import="java.io.FileOutputStream"%> <%@page import="net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter"%> <%@page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%> <%@page import="java.net.URLEncoder"%> <%@page import="net.sf.jasperreports.engine.JRException"%> <%@page import="net.sf.jasperreports.j2ee.servlets.ImageServlet"%> <%@page import="java.io.IOException"%> <%@page import="java.io.OutputStream"%> <%@page import="net.sf.jasperreports.engine.JRExporterParameter"%> <%@page import="net.sf.jasperreports.engine.export.JExcelApiExporter"%> <%@page import="net.sf.jasperreports.engine.JasperPrint"%> <% Object obj = session.getAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE); JasperPrint jasperPrint = null; if(obj instanceof JasperPrint){ jasperPrint = (JasperPrint)obj; } String fileNameDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); String outputPath = application.getRealPath("/") + "download2/"; //判断下载文件夹是否存在,如果不存在则创建 File downloadFilePath = new File(outputPath); if(!downloadFilePath.exists()){ downloadFilePath.mkdir(); } String downloadXlsFileName = "xlsReport-" + fileNameDate + ".xls"; String downloadPdfFileName = "xlsReport-" + fileNameDate + ".pdf"; OutputStream excelOutputStream = null; InputStream excelInputStream = null; InputStream pdfFileInputStream = null; OutputStream pdfResponseOutputStream = null; File inputXlsFile = null;//输入的xls文件 File outputPdfFile = null;//输出的pdf文件 try { excelOutputStream = new FileOutputStream(outputPath + downloadXlsFileName); JExcelApiExporter xlsExporter = new JExcelApiExporter(); xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,excelOutputStream); xlsExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); xlsExporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS,Boolean.TRUE); xlsExporter.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); //xlsExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);//设置导出的Excel 数字显示成字符串问题 //xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE); //xlsExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE); xlsExporter.exportReport(); excelOutputStream.close(); excelOutputStream.flush(); inputXlsFile = new File(outputPath + downloadXlsFileName); outputPdfFile = new File(outputPath + downloadPdfFileName); excelInputStream = new FileInputStream(inputXlsFile); ExcelToPdf.excelToPdf(excelInputStream, outputPath + downloadPdfFileName); try { pdfFileInputStream = new FileInputStream(outputPath + downloadPdfFileName); pdfResponseOutputStream = response.getOutputStream(); response.setContentType("application/pdf"); //response.setCharacterEncoding("UTF-8"); String downloadFileName = session.getAttribute("fileName") + ".pdf"; response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(downloadFileName, "UTF-8")); byte[] b = new byte[1024]; int read; while((read = pdfFileInputStream.read(b)) != -1){ pdfResponseOutputStream.write(b, 0, 1024); } }catch (IOException e) { e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } } catch (JRException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try{ //删除临时生成的两个文件 try{ inputXlsFile.delete(); outputPdfFile.delete(); }catch(Exception e){ e.printStackTrace(); } pdfResponseOutputStream.close(); pdfResponseOutputStream.flush(); out.clear(); out = pageContext.pushBody(); }catch(Exception e){ e.printStackTrace(); } } %>