Excel PDF报表的导出
程序员文章站
2024-01-04 13:01:34
...
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();
});