Excel PDF報表的匯出
阿新 • • 發佈:2018-12-04
1.Excel---PDF
@Action("report_exportXls") public String exportXls() throws IOException { // 查詢出 滿足當前條件 結果資料 List<WayBill> wayBills = wayBillService.findWayBills(model); // 生成Excel檔案 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet sheet = hssfWorkbook.createSheet("運單資料"); // 表頭 HSSFRow headRow = sheet.createRow(0); headRow.createCell(0).setCellValue("運單號"); headRow.createCell(1).setCellValue("寄件人"); headRow.createCell(2).setCellValue("寄件人電話"); headRow.createCell(3).setCellValue("寄件人地址"); headRow.createCell(4).setCellValue("收件人"); headRow.createCell(5).setCellValue("收件人電話"); headRow.createCell(6).setCellValue("收件人地址"); // 表格資料 for (WayBill wayBill : wayBills) { HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1); dataRow.createCell(0).setCellValue(wayBill.getWayBillNum()); dataRow.createCell(1).setCellValue(wayBill.getSendName()); dataRow.createCell(2).setCellValue(wayBill.getSendMobile()); dataRow.createCell(3).setCellValue(wayBill.getSendAddress()); dataRow.createCell(4).setCellValue(wayBill.getRecName()); dataRow.createCell(5).setCellValue(wayBill.getRecMobile()); dataRow.createCell(6).setCellValue(wayBill.getRecAddress()); } // 下載匯出 // 設定頭資訊 ServletActionContext.getResponse().setContentType( "application/vnd.ms-excel"); String filename = "運單資料.xls"; String agent = ServletActionContext.getRequest() .getHeader("user-agent"); filename = FileUtils.encodeDownloadFilename(filename, agent); ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename); ServletOutputStream outputStream = ServletActionContext.getResponse() .getOutputStream(); hssfWorkbook.write(outputStream); // 關閉 hssfWorkbook.close(); return NONE; } @Action("report_exportPdf") public String exportPdf() throws IOException, DocumentException { // 查詢出 滿足當前條件 結果資料 List<WayBill> wayBills = wayBillService.findWayBills(model); // 下載匯出 // 設定頭資訊 ServletActionContext.getResponse().setContentType("application/pdf"); String filename = "運單資料.pdf"; String agent = ServletActionContext.getRequest() .getHeader("user-agent"); filename = FileUtils.encodeDownloadFilename(filename, agent); ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename); // 生成PDF檔案 Document document = new Document(); PdfWriter.getInstance(document, ServletActionContext.getResponse() .getOutputStream()); document.open(); // 寫PDF資料 // 向document 生成pdf表格 Table table = new Table(7); table.setWidth(80); // 寬度 table.setBorder(1); // 邊框 table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // 水平對齊方式 table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); // 垂直對齊方式 // 設定表格字型 BaseFont cn = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); Font font = new Font(cn, 10, Font.NORMAL, Color.BLUE); // 寫表頭 table.addCell(buildCell("運單號", font)); table.addCell(buildCell("寄件人", font)); table.addCell(buildCell("寄件人電話", font)); table.addCell(buildCell("寄件人地址", font)); table.addCell(buildCell("收件人", font)); table.addCell(buildCell("收件人電話", font)); table.addCell(buildCell("收件人地址", font)); // 寫資料 for (WayBill wayBill : wayBills) { table.addCell(buildCell(wayBill.getWayBillNum(), font)); table.addCell(buildCell(wayBill.getSendName(), font)); table.addCell(buildCell(wayBill.getSendMobile(), font)); table.addCell(buildCell(wayBill.getSendAddress(), font)); table.addCell(buildCell(wayBill.getRecName(), font)); table.addCell(buildCell(wayBill.getRecMobile(), font)); table.addCell(buildCell(wayBill.getRecAddress(), font)); } // 將表格加入文件 document.add(table); document.close(); return NONE; } private Cell buildCell(String content, Font font) throws BadElementException { Phrase phrase = new Phrase(content, font); return new Cell(phrase); } @Action("report_exportJasperPdf") public String exportJasperPdf() throws IOException, DocumentException, JRException, SQLException { // 查詢出 滿足當前條件 結果資料 List<WayBill> wayBills = wayBillService.findWayBills(model); // 下載匯出 // 設定頭資訊 ServletActionContext.getResponse().setContentType("application/pdf"); String filename = "運單資料.pdf"; String agent = ServletActionContext.getRequest() .getHeader("user-agent"); filename = FileUtils.encodeDownloadFilename(filename, agent); ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename); // 根據 jasperReport模板 生成pdf // 讀取模板檔案 String jrxml = ServletActionContext.getServletContext().getRealPath( "/WEB-INF/jasper/waybill.jrxml"); JasperReport report = JasperCompileManager.compileReport(jrxml); // 設定模板資料 // Parameter變數 Map<String, Object> paramerters = new HashMap<String, Object>(); paramerters.put("company", "百度"); // Field變數 JasperPrint jasperPrint = JasperFillManager.fillReport(report, paramerters, new JRBeanCollectionDataSource(wayBills)); System.out.println(wayBills); // 生成PDF客戶端 JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ServletActionContext.getResponse().getOutputStream()); exporter.exportReport();// 匯出 ServletActionContext.getResponse().getOutputStream().close(); return NONE; } }
2.html匯出操作
// 匯出Excel 按鈕 $("#exportXlsBtn").click(function(){ // 下載效果 $("#searchForm").attr("action", "../../report_exportXls.action"); $("#searchForm").submit(); }); // 匯出 PDF 按鈕 $("#exportPdfBtn").click(function(){ // 下載效果 $("#searchForm").attr("action", "../../report_exportPdf.action"); $("#searchForm").submit(); }); // 結合模板 匯出 PDF 按鈕 $("#exportJasperPdfBtn").click(function(){ // 下載效果 $("#searchForm").attr("action", "../../report_exportJasperPdf.action"); $("#searchForm").submit(); });