/**
* 导出pdf
* @param list
*/
public void pdfExport(List<FaHuoQingDan> list) {
File file = new File("E:\\1.jasper");
JasperReport report = null;
JasperPrint jasperPrint = null;
try {
report = (JasperReport) JRLoader.loadObject(file);
} catch (JRException e1) {
e1.printStackTrace();
}
JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
Map<String, Object> params = new HashMap<String, Object>();
if(report!=null) {
try {
jasperPrint = JasperFillManager.fillReport(report, params, jrDataSource);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"E:\\1.pdf");
try {
pdfExporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}
这里List里封装的是实体类,jasper是已经设计好的模板,模板就是根据实体类的属性设置的。这样通过调用相关方法,把List里的数据添加到模板中的相应位置,然后转成pdf文件。