使用IText PDF 类库导出:
1.导入依赖:
<dependency>
<groupId>com.lowagiegroupId>
<artifactId>itextartifactId>
<version>4.2.1version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>itext-asianartifactId>
<version>5.2.0version>
dependency>
2.查询数据:
List wayBills = wayBillService.findAll(model);
3.导出pdf文件:
// 设置头信息
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(100); // 宽度
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.HELVETICA, Color.BLACK);
// 写表头
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();
JasperReport+IReport导出:
1.添加依赖:
<dependency>
<groupId>org.codehaus.groovygroupId>
<artifactId>groovy-allartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>net.sf.jasperreportsgroupId>
<artifactId>jasperreportsartifactId>
<version>5.6.0version>
<exclusions>
<exclusion>
<groupId>com.lowagiegroupId>
<artifactId>itextartifactId>
exclusion>
exclusions>
dependency>
2.编写查询代码:
// 下载导出
// 设置头信息
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 paramerters = new HashMap();
paramerters.put("company", "传智播客");
// Field变量
//根据要求查询的数据生成PDF
JasperPrint jasperPrint = JasperFillManager.fillReport(report,
paramerters, new JRBeanCollectionDataSource(wayBills));
//根据IReport的SQL查询数据生成PDF
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report,
paramerters, dataSource.getConnection());
//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();// 导出
ps:
当我们不使用IReoort查询数据显示时,需要在ireport 添加符合查询对象属性的 Field
PDF没有数据:检查前台表单的提交方式是否是post;
PDF数据没有自动换行:检查在属性中是否设置了自动换行,手动在IReport中拓展行高,有没有为显示数据添加中文显示;