Jaspersoft studio工具(報表生成工具)之 設定PDF 的開發
JasperReport 導出 PDF 的設定
JasperReport生命周期(重點):
通過JasperReports生成PDF報表一共要經過三個階段,我們稱之為 JasperReport的生命周期,這三個階段為:
設計(Design)階段、定義模闆(用工具 Jaspersoft studio來定義模闆)
執行(Execution)階段,模闆 + 資料(填充)
輸出(Export)階段,展示。 将模闆和資料一起展示
JasperReport執行流程(重點):
java整合jasper模闆檔案
jasper的PDF模闆由Jaspersoft studio工具生成,但是得注意:
1.記得儲存(ctrl+s)模闆
2.修改模闆字型和idea中支援的字型要吻合,這樣才能顯示中文
在idea中項目的resources目錄下要導入配置,檔案在:D:\Astudy\軟體語言\Java\java就\項目一\13.saax-export_day13\03.資料和工具\03 中文字型
步驟
1.在工具裡面生成PDF模闆并且編譯,把編譯後的.jsaper檔案拷貝進idea項目中
2.編寫代碼,實作資料填充
注意:資料填充 由三種方式
方式一,參數map填充
@RequestMapping("/exportPdf")
public void exportPdf(String id) throws Exception {
//1.讀取模闆檔案(.jasper)
InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/test02_map.jasper");
//2.把資料填充到模闆
//進行Map參數填充
HashMap map = new HashMap<>();
//模闆的參數名稱和Map的key一緻的
map.put("userName","小蒼");
map.put("email","[email protected]");
map.put("companyName","位元組跑動");
map.put("deptName","視訊組");
JasperPrint print = JasperFillManager.fillReport(inputStream, map , new JREmptyDataSource());
//3.導出PDF檔案
JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
}
方式二,JDBC資料源填充
@Autowired
private DataSource dataSource;
@RequestMapping("/exportPdf")
public void exportPdf(String id) throws Exception {
//1.讀取模闆檔案(.jasper)
InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/test03_jdbc.jasper");
//2.把資料填充到模闆
JasperPrint print = JasperFillManager.fillReport(inputStream, new HashMap<>() ,dataSource.getConnection());
//3.導出PDF檔案
JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
}
方式三,JavaBean資料源填充
@RequestMapping("/exportPdf")
public void exportPdf(String id) throws Exception {
//1.讀取模闆檔案(.jasper)
InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/test04_list.jasper");
//2.把資料填充到模闆
List list = new ArrayList();
//注意: 必須遵守的規則:List的JavaBean的每個屬性名稱 和 模闆的Filed名稱 一緻
for(int i=0;i<10;i++){
User user = new User();
user.setUserName("小蒼"+i);
user.setEmail("xc+"+i+"@itcast.cn");
user.setCompanyName("位元組跑動");
user.setDeptName("視訊組"+i);
list.add(user);
}
//進而得到資料源
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
JasperPrint print = JasperFillManager.fillReport(inputStream, new HashMap<>() ,dataSource);
//3.導出PDF檔案
JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
}
Map 參數填充一般用在非表格(table)資料填充(關鍵是:隻有一條資料)
資料源 參數填充一般用在表格(table)資料填充(關鍵是:n行資料)
資料源 填充資料分為:JDBC資料源填充資料(資料庫連接配接)、JavaBean填充資料(list集合
對于JDBC資料源,是在做PDF模闆的時候就連結了資料庫擷取資料,再在代碼裡通過資料源建立連接配接對象進而填充
對于JaveBean資料源(又稱為List集合填充),隻是在PDF模闆中填充空的資料,真正的資料還是在代碼中填充
其實在java代碼中無非就是三步
1.讀取模闆檔案(.jasper) InputStream inputStream = session.getServletContext().getResourceAsStream(“/jasper/test04_list.jasper(webapp下面的檔案位址)”);
2.把資料填充到模闆
JasperPrint print = JasperFillManager.fillReport (inputStream, Map集合 , dataSource資料源);
填充就是在用資料源還是map集合的不同而分出的3中方法
如果是用了map集合,那資料源就得為空,JasperPrint print = JasperFillManager.fillReport (inputStream, Map集合 , new JREmptyDataSource());
如果用了資料源,那map集合就得為空,JasperPrint print = JasperFillManager.fillReport (inputStream, new HashMap<>() , 資料源);
在區分,①用JDBC資料源,那就是JasperPrint print = JasperFillManager.fillReport(inputStream, new HashMap<>() ,dataSource.getConnection());
記得在外面寫上 @Autowired
private DataSource dataSource;
②用JavaBean資料源,那就是
//進而得到資料源
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
JasperPrint print = JasperFillManager.fillReport (inputStream, new HashMap<>() , dataSource);
3.pdf檔案的顯示或導出
①在浏覽器顯示
JasperExportManager.exportReportToPdfStream( print, response.getOutputStream());
②下載下傳
* 參數:封裝好模闆好資料的JasperPrint對象
* 參數二:輸出的位置
//設定下載下傳框響應頭
response.setHeader(“Content-Disposition”,”attachment;filename=export.pdf”); /參數一是響應頭,參數而,是附件和下載下傳後的檔案名
JasperExportManager.exportReportToPdfStream( print, response.getOutputStream()); //這一步同上