Jasper报告与excel数据源
问题描述:
我使用jasper设计器(不是iReport而是eclipse插件)创建了一个使用excel文件作为数据源的报告。
报告工作正常,在设计器中,并阅读来自文件的数据没有问题但是在将文件编译为 file.jasper
之后,给了他excel文件的路径,在JasperViewer中没有任何东西!
这是我的代码:
I have created, using a jasper designer (not iReport but a plugin for eclipse), a report that uses an excel file as datasource.
the report works fine, in the designer, and read the data from the file without problems but after compiling the file to file.jasper
and giving him the excel file's path nothing apears in the JasperViewer!
This is my code:
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();
}
答
解决方案:
此代码完美无缺:
SOLUTION:
This code woks perfect:
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();
}