Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
848 views
in Technique[技术] by (71.8m points)

java - JasperReports export to xlsx, not xls

I can't find how to export a file in .xlsx in JasperReports 4.1.1. The class:

JRXlsExporter

has not a Xlsx equivalent. And i can't find a parameter to set the output format from xls to xlsx.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The JRXlsxExporter class should be used for exporting at XLSX format.

Sample of using exporter with JasperReports prior 5.5.2 version

Till JasperReports 5.5.1 this code can be used for generating report at xlsx format:

JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data);

JasperReport jasperReport = JasperCompileManager.compileReport(reportJRXMLSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, outputFileName);

exporter.exportReport();

Since 5.5.2 version of library the JRAbstractExporter.setParameter(JRExporterParameter, Object) method is deprecated.

Sample of using exporter with modern JasperReports versions

In this example I used JRS 6.4.1 version:

JasperReport jasperReport;
try (InputStream inputStream = JRLoader.getResourceInputStream(jrxmlFilePath)) {
    jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream));
}
Map<String, Object> params = new HashMap<>();

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setOnePagePerSheet(true);
configuration.setIgnoreGraphics(false);

File outputFile = new File("output.xlsx");
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     OutputStream fileOutputStream = new FileOutputStream(outputFile)) {
    Exporter exporter = new JRXlsxExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    byteArrayOutputStream.writeTo(fileOutputStream);
}

Instead of using JRExporter.setParameter method we have to use implementation of XlsReportConfiguration interface. In example above I used SimpleXlsxReportConfiguration implementation of XlsReportConfiguration for defining settings specific to JRXlsxExporter exporter.


More information


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...