0
我正嘗試使用Java將數據傳遞給JasperReport。但是,表數據非常動態,我無法將SQL查詢傳遞給報表。任何想法呢? 我有一個二維數組對象類型,我有所有的數據。我怎麼能通過這個?如何使用Java在Jasper Report表中添加數據
ConnectionManager con = new ConnectionManager();
con.establishConnection();
String fileName = "Pmc_Bill.jrxml";
String outFileName = "OutputReport.pdf";
HashMap params = new HashMap();
params.put("PName", pname);
params.put("PSerial", psrl);
params.put("PGender",pgen);
params.put("PPhone",pph);
params.put("PAge",page);
params.put("PRefer",pref);
params.put("PDateR",dateNow);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(fileName);
if(jasperReport != null)
System.out.println("so far so good ");
// Fill the report using an empty data source
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JRTableModelDataSource(tbl.getModel()));//con.connection);
try{
JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName);
System.out.printf("File exported sucessfully");
}catch(Exception e){
e.printStackTrace();
}
JasperViewer.viewReport(jasperPrint);
} catch (JRException e) {
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
System.exit(1);
}
你是什麼意思「的表中的數據是非常動態的,多數民衆贊成Y不能通過SQL查詢」是什麼意思?無論如何,如果您的數據保留在數據庫中,您仍然需要查詢。 – dcernahoschi
您可以閱讀[數據源示例](http://jasperreports.sourceforge.net/sample.reference/datasource/index.html#datasources)文章。 *** JRMapArrayDataSource ***,*** JRMapCollectionDataSource ***,*** ListOfArrayDataSource ***,*** JRBeanArrayDataSource ***,*** JRBeanCollectionDataSource ***數據源的實現是您的代碼的良好選擇。 –