[英]Jasper report with excel data source
我使用一個碧玉設計器(不是iReport,而是一個eclipse插件)創建了一個使用excel文件作為數據源的報告。
該報告在設計器中可以正常工作,並且可以毫無問題地從文件中讀取數據,但是將文件編譯為file.jasper
並為他提供了excel文件的路徑后,JasperViewer不會出現任何錯誤!
這是我的代碼:
try{
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("DataFile", "jasper_export.xls");
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(new File("file.jasper")), parameters,conn);
JasperViewer jv = new JasperViewer(jasperPrint, false);
jv.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
解:
這段代碼非常完美:
try{
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("param_name", paramValue);
ExcelDataSource ds = new ExcelDataSource(JRLoader.getLocationInputStream(excelFilePath));
String[] columnNames = new String[]{"id", "nom", "iden", "adress", "activity", "compta"};
ds.setColumnNames(columnNames);
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(new File(yourJasperFilePath)), parameters, ds);
JasperPrintManager.printReport(jasperPrint, false);
} catch (Exception ex) {
ex.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.