2013-04-04 118 views
0

我試圖以excel(xls)格式導出報告,並在打開/保存對話框幫助下從瀏覽器中下載文件。JasperReports:將報告導出到Excel時文件名不正確

當文件下載框彈出時,文件名不能正確顯示。該文件名來作爲URL

下面是我的代碼:

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportMCQ, params, datasource); 

JRXlsExporter jasperXlsExportMgr = new JRXlsExporter(); 

ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); 
jasperXlsExportMgr.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, java.lang.Boolean.FALSE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, java.lang.Boolean.FALSE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java.lang.Boolean.TRUE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, java.lang.Boolean.TRUE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, java.lang.Boolean.TRUE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, java.lang.Boolean.FALSE); 
jasperXlsExportMgr.setParameter(JRXlsExporterParameter.CREATE_CUSTOM_PALETTE, java.lang.Boolean.TRUE); 
jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); 
jasperXlsExportMgr.exportReport(); 
bytes = xlsReport.toByteArray(); 
getResponse().setHeader("Content-disposition", "attachment; filename=\"report.xls\""); 
getResponse().setContentType("application/vnd.ms-excel"); 
getResponse().setContentLength(bytes.length); 
if (bytes.length > 0) { 
    servletOutputStream.write(bytes, 0, bytes.length); 
    servletOutputStream.flush(); 
    servletOutputStream.close(); 
} 
+0

可能重複[?我怎樣才能改變一個JasperReport的實際名稱](http://stackoverflow.com/questions/6738253/how-can-i-change-a -jasperreports-actual-name)&[JasperReports PdfServlet以PDF格式保存報告 - 我如何設置瀏覽器的文件名以提示用戶?](http://stackoverflow.com/q/12095833/876298) – 2013-04-04 07:33:31

回答

1

試試這個參數:

jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, 「MyFile的」);

從碧玉文件:

靜態JRExporterParameter輸出文件名:導出報表的文件名。

0

試試這個代碼:

String dir = "C:/myproject/"; 
Connection kns = BukaKoneksi(url); 
HashMap hm = new HashMap(); 
String name = dir+"file/_.xls""; 
String nfl = dir+"file/__.xls"; 
String fjas = "a.jasper"; 
String nname = "a.xls"; 

//jasper 
JasperPrint print = JasperFillManager.fillReport(dir+"jasper/"+fjas, hm, kns); 
JRXlsExporter exporter = new JRXlsExporter(); 
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); 
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); 
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, fname); 
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, nfl); 
exporter.exportReport();  

//download file 
byte bytes[] = xlsReport.toByteArray(); 
response.setContentType("application/vnd.ms-excel"); 
response.setHeader("Content-Disposition", "attachment; filename="+nname); 
response.setContentLength(bytes.length); 
xlsReport.close(); 
OutputStream ouputStream = response.getOutputStream(); 
ouputStream.write(bytes, 0, bytes.length); 
ouputStream.flush(); 
ouputStream.close();