java - 如何将 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();*/
解决方案
推荐阅读
- xcode - 使用 xcode 上传存档:前缀或后缀无效
- python - 错误:预期的 pk 值,收到的外键字段列表
- java - Scoped Variables 和 WeakReferences 奇怪地交互 - 一些对象没有被垃圾收集
- ruby-on-rails - Phusion 乘客是否重用流程?
- node.js - 如何获取 Firebase 的数据库键和值
- c++ - 在 C++14 中避免字段零初始化
- angular - Web 应用程序 - 浏览器/计算机识别
- java - 我应该得到“无效”作为输出,但我得到“有效”作为输出,不知道为什么输出错误
- json - 如何使用 Moment js 将 Json 对象日期转换为原生反应?
- python - dataframe.to_sql 导入 Teradata(此用户无权在 LABUSERS 上创建) Datalab 表名