我需要在jasper报告中导出为excel和csv格式。对于我通过创建自定义类(使用api)尝试过的excel,它没有导出,但保存并取消了弹出窗口,文件类型未知。.]
知道为什么会发生吗?
用作JRXlsExporter导出到XSL,并用作JRCsvExporter导出为CSV。
在大多数情况下,无需创建自定义类。
编辑
该类存储在jar poi-3.5-FINAL-20090928.jar中,该文件应位于您的“ iReportInstallationFolder” \ modules \ ext \中
对我来说是C:\Program Files\Jaspersoft\iReport-3.7.4\ireport\modules\ext\
名称可能有所不同,但应为poi-3.5-FINAL-*。jar。
使它包含在您的类路径中,应该没问题。
您可以从Apache Poi主页下载jar。
这里是我从其站点http://archive.apache.org/dist/poi/release/bin/poi-bin-3.5-FINAL-20090928.tar.gz到罐子的链接
对于使用JRCsvExporter的用户,以下代码可能有用。提供一些示例结构。
Spring框架服务类(CSVExportService.java):
public JasperPrint getRawData(String empIds) {
JasperPrint jp = null;
String reportName = "Employee Report";
// use your own method to get empList
// eg: List<Employee> empList = empServiceClass.findByEmpIds(empIds);
JRDataSource jrDataSource = new JRBeanCollectionDataSource(empList);
// build your report
DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();
dynamicReportBuilder.setAllowDetailSplit(false);
// configure your report with few more options here
// create columns
ColumnBuilder columnBuilderName = ColumnBuilder.getNew();
columnBuilderName.setTitle("Emp Name");
columnBuilderName.setWidth(300);
columnBuilderName.setFixedWidth(true);
columnBuilderName.setColumnProperty("name", String.class.getName());
dynamicReportBuilder.addColumn(columnBuilderName.build());
DynamicReport dynamicReport = dynamicReportBuilder.build();
jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), jrDataSource, new HashMap<String, Object>());
return jp;
}
Spring框架控制器类:
public void exportToCSV(@PathVariable String empIds){
JasperPrint jp = null;
jp = csvExportService.getRawData(empIds);
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename="EMPRawData.csv");
OutputStream out = response.getOutputStream();
JRCsvExporter exporterCSV = new JRCsvExporter();
exporterCSV.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporterCSV.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporterCSV.exportReport();
out.flush();
}
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new FileOutputStream(new File(output1))));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
//configuration.setWriteBOM(Boolean.TRUE);
exporter.setConfiguration(configuration);
exporter.exportReport();
请使用此代码。它可以正常工作