Java 如何生成、导出为word docx文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22968303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:41:27  来源:igfitidea点击:

How to generate, export to word docx file?

javajasper-reportsdocxexport-to-word

提问by eeijlar

I am trying to generate a docxin jasper report. I have this code:

我正在尝试生成一个docxin jasper 报告。我有这个代码:

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

How do I write the report out to file? Most of the examples I have seen are all around using servlets.

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

采纳答案by user432

Add the parameter JRExporterParameter.OUTPUT_FILE_NAMEto specify the file and remove the parameter JRExporterParameter.OUTPUT_STREAM.

添加参数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();

回答by Petter Friberg

JRExporterParameteris deprecatedsince jasper version 5.6

JRExporterParameter自 jasper 5.6 版起已弃用

The current way since this version would be:

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

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