我需要在jasper報告中導出爲excel和csv格式。 爲Excel我試圖通過(使用API)創建自定義類,但是它沒有出口,東西被保存並取消彈出窗口與文件類型未知的未來..如何在jasper報告中導出爲excel和csv格式
任何想法,爲什麼這是怎麼回事?
我需要在jasper報告中導出爲excel和csv格式。 爲Excel我試圖通過(使用API)創建自定義類,但是它沒有出口,東西被保存並取消彈出窗口與文件類型未知的未來..如何在jasper報告中導出爲excel和csv格式
任何想法,爲什麼這是怎麼回事?
用作JRXlsExporter出口到XSL和CSV一個JRCsvExporter。
應該沒有必要建立在大多數情況下,自定義類。
編輯
類存儲罐POI-3.5-FINAL-20090928.jar,應該在你的 「iReportInstallationFolder」 \模塊\分機可以找到\
對於我來說,C:\Program Files\Jaspersoft\iReport-3.7.4\ireport\modules\ext\
內
的名稱可能是不同的,但應該是POI-3.5-FINAL - *罐子。
使它包含在你的classpath中,你應該沒問題。
您可以從Apache Poi主頁下載的罐子。
這裏是一個罐子,我從他們的網站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();
}
如果我的應用程序沒有響應(HttpServletResponse)會怎麼樣? 我如何創建文件? – bynu022 2017-03-22 09:00:07
@Gordon ..我得到了像「的服務之一拋出uncaught異常異常Servlet的方法:InvestorConfirmServlet。拋出的異常:java.lang.NoClassDefFoundError:org.apache.poi.hssf.usermodel.HSSFCellStyle「..我把.jar文件放在類路徑中。但即使它會引發錯誤... – Manu 2010-09-17 08:50:52
@Manu我更新了我的答案。希望應該幫助。 – Gordon 2010-09-17 09:29:55
你可以給我下載jar文件... – Manu 2010-09-17 09:43:41