如何生成,导出到word docx文件?

时间:2014-04-09 16:15:41

标签: java jasper-reports docx export-to-word

我正在尝试在jasper报告中生成docx。我有这段代码:

JRDocxExporter exporter = new JRDocxExporter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();    
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport(); 

如何将报告写入文件?我见过的大多数例子都是使用servlet。

2 个答案:

答案 0 :(得分:7)

添加参数JRExporterParameter.OUTPUT_FILE_NAME以指定文件并删除参数JRExporterParameter.OUTPUT_STREAM

JRDocxExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "myreport.docx");
exporter.exportReport();

答案 1 :(得分:6)

  

JRExporterParameter 已弃用,因为jasper版本5.6

此版本以来的当前方式是:

JRDocxExporter export = new JRDocxExporter();
export.setExporterInput(new SimpleExporterInput(jasperPrint));
export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File("path/toMy/report.docx")));

SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
//config.setFlexibleRowHeight(true); //Set desired configuration

export.setConfiguration(config);            
export.exportReport();
相关问题