首页 > 解决方案 > 如何将 JasperReport 导出为使用 Java 代码生成的 excel 格式以 base64 编码的字符串并对其进行解码

问题描述

我能够生成 Jasper 报告文件并将其导出到 excel 文件,同时将其保存到磁盘(它可以按预期工作并正确显示数据)。如果不是文件作为输出,而是使用 ByteArrayOutputStream 作为输出,我会得到一个字节数组。如果我将流编码为 Base64 并进一步解码并将字符串保存在 xlsx 文件中,它会显示损坏的数据。

{
//some code....
ReportService reportService = new ReportService();
JasperPrint jp = rawreport.getReport();
data = reportService.getReportXlsx(jp);//function defined below
BASE64Encoder base64Encoder = new BASE64Encoder();
String encckycstr = base64Encoder.encodeBuffer(data);
//If I put encckycstr into a new text file and save it has xlsx
String destinationPath = "E:\\Destination\\data.xls";
//decode Base64 String to image
FileOutputStream fos = new FileOutputStream(destinationPath); 
byte[] bytes = new BASE64Decoder().decodeBuffer(encckycstr);
fos.write(bytes);
fos.close();
//when I open data.xls, it doesnt open in excel or openoffice calc.
//some code.....

}

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Locale;

import org.springframework.stereotype.Service;

import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.DefaultFormatFactory;
import net.sf.jasperreports.engine.util.DurationNumberFormat;
import net.sf.jasperreports.engine.xml.JRGenericPrintElementParameterFactory.Parameter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;

public byte[] getReportXlsx(final JasperPrint jp) throws JRException, 
IOException {
    JRXlsxExporter xlsxExporter = new JRXlsxExporter();
    final byte[] rawBytes;

    //for byte array export
    try(ByteArrayOutputStream xlsReport = new ByteArrayOutputStream()){
        jp.setProperty(net.sf.jasperreports.engine.JRParameter.IS_IGNORE_PAGINATION, "true");
        xlsxExporter.setExporterInput(new SimpleExporterInput(jp));
        xlsxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport));
        SimpleXlsxReportConfiguration xlsxreportConfig = new SimpleXlsxReportConfiguration();
        xlsxreportConfig.setSheetNames(new String[] { "Some Report" });
        xlsxreportConfig.setRemoveEmptySpaceBetweenRows(true);
        xlsxreportConfig.setForcePageBreaks(false);
        xlsxreportConfig.setWrapText(false);
        xlsxreportConfig.setCollapseRowSpan(true);
        xlsxExporter.setConfiguration(xlsxreportConfig);
        xlsxExporter.exportReport();

        rawBytes = xlsReport.toByteArray();
    }

//The excel file is generated and can be opened if I give filename string as input to SimpleOutputStreamExporterOutput 
//which I believe uses file stream internally, but not when using ByteArrayOutputStream as input as used above

    /*JRXlsxExporter xlsexporter = new JRXlsxExporter();
    xlsexporter.setExporterInput(new SimpleExporterInput(jp));
    xlsexporter.setExporterOutput(new SimpleOutputStreamExporterOutput("Somedatareport.xlsx"));
    SimpleXlsxReportConfiguration xlsxreportConfig = new SimpleXlsxReportConfiguration();
    xlsxreportConfig.setSheetNames(new String[] { "Some Report" });
    xlsxreportConfig.setRemoveEmptySpaceBetweenRows(true);
    xlsxreportConfig.setForcePageBreaks(false);
    xlsxreportConfig.setWrapText(false);
    xlsxreportConfig.setCollapseRowSpan(true);
    xlsexporter.setConfiguration(xlsxreportConfig);
    xlsexporter.exportReport();*/

标签: javajasper-reportsexport-to-excel

解决方案


推荐阅读