【问题标题】:JasperReports: File Name incorrect while exporting report to ExcelJasperReports:将报告导出到 Excel 时文件名不正确
【发布时间】:2013-04-04 15:38:55
【问题描述】:

我正在尝试以 excel (xls) 格式导出报告,并在打开/保存对话框的帮助下提供从浏览器下载文件的选项。

当文件下载框弹出时,文件名不正确。文件名以 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();
}

【问题讨论】:

标签: java jasper-reports export-to-excel


【解决方案1】:

试试这个参数:

jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "MyFile");

来自 Jasper 文档:

static JRExporterParameter OUTPUT_FILE_NAME:导出报告的文件名。

【讨论】:

    【解决方案2】:

    试试这个代码:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多