Java Jasper报告如何使用RTL工作表创建excel xlsx文件?
我们使用的是jasper版本6。我们可以导出到EXCEL(XLS和XLSX) 以下代码适用于XLS并创建RTL工作表:Java Jasper报告如何使用RTL工作表创建excel xlsx文件?,java,jasper-reports,export-to-excel,Java,Jasper Reports,Export To Excel,我们使用的是jasper版本6。我们可以导出到EXCEL(XLS和XLSX) 以下代码适用于XLS并创建RTL工作表: exporter = new JRXlsExporter(); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out)); SimpleXlsReportConfiguration xlsReportConfig = new SimpleXlsReportConfiguration();
exporter = new JRXlsExporter();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsReportConfiguration xlsReportConfig = new SimpleXlsReportConfiguration();
xlsReportConfig.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsReportConfig);
但是,当我尝试使用相同的代码制作XLSX文件时,图纸方向不会更改为RTL:
exporter = new JRXlsxExporter();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsxReportConfiguration xlsxReportConfiguration = new SimpleXlsxReportConfiguration();
xlsxReportConfiguration.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsxReportConfiguration);
在使用v6.1.1测试的jasper报告库中,似乎是一个bug,导出后添加下面的代码将正常工作(jasper报告分发中包含poi库,因此poi中没有bug…)
//out是jasper报告导出后的文件
XSSF工作簿=新XSSF工作簿(新文件输入流(输出));
int ns=workbook.getNumberOfSheets();
对于(int i=0;i
在最新版本的JasperReports 6.1中,我在XLSX中生成报告时遇到问题,但这段代码适合我: 首先,我配置jasper打印
JRSwapFile swapFile = new JRSwapFile(".", 1024, 1024);
JRVirtualizer virtualizer = new JRSwapFileVirtualizer(100, swapFile, true);
parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
JasperPrint print = JasperFillManager.fillReport(stream, parameters, dbConnection);
List<JasperPrint> prints = new ArrayList<JasperPrint>();
prints.add(print);
我创建了一个JRXlsxExporter实例,用于生成扩展名为.xslx的文件,并将打印机和输出放入:
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(prints));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(output));
下一步是为导出器创建报告配置,当我输入此代码时,我的报告工作!,所以你必须使用它
SimpleXlsxReportConfiguration xlsxReportConfiguration = new SimpleXlsxReportConfiguration();
xlsxReportConfiguration.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsxReportConfiguration);
最后,生成报告并关闭outputStream:
exporter.exportReport();
output.flush();
output.close();
我希望这适用于您我最后使用了下面的代码(虽然成本很高,但效果很好),在
哪一个版本的Jasper report是您已经厌倦的?在v6.1.1中发现了相同的错误
SimpleXlsxReportConfiguration xlsxReportConfiguration = new SimpleXlsxReportConfiguration();
xlsxReportConfiguration.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsxReportConfiguration);
exporter.exportReport();
output.flush();
output.close();
public class ReportUtils {
private ReportUtils(){
}
/**
* mirror each page layout
* @param print
*/
public static void mirrorLayout(JasperPrint print) {
int pageWidth = print.getPageWidth();
for (Object element : print.getPages()) {
JRPrintPage page = (JRPrintPage) element;
mirrorLayout(page.getElements(), pageWidth);
}
}
/**
* mirror a list of elements
* @param print
*/
protected static void mirrorLayout(List<?> elements, int totalWidth) {
for (Iterator<?> it = elements.iterator(); it.hasNext();) {
JRPrintElement element = (JRPrintElement) it.next();
int mirrorX = totalWidth - element.getX() - element.getWidth();
element.setX(mirrorX);
if (element instanceof JRPrintFrame) {
JRPrintFrame frame = (JRPrintFrame) element;
mirrorLayout(frame.getElements(), frame.getWidth());
}
}
}
}
Exporter exporter;
ByteArrayOutputStream out = new ByteArrayOutputStream();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
JasperPrint jasperPrint = JasperFillManager.fillReport(report,
params, dataSource != null ? new JRMapArrayDataSource(
dataSource) : new JREmptyDataSource());
ReportUtils.mirrorLayout(jasperPrint);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.exportReport();
return out.toByteArray();