简体   繁体   English

Jasper Report 如何使用 RTL 表创建 excel xlsx 文件?

[英]Jasper Report how to create an excel xlsx file with RTL sheet?

We are using jasper version 6. We can export to EXCEL ( XLS and XLSX) .我们使用的是 jasper 版本 6。我们可以导出到 EXCEL(XLS 和 XLSX)。

Below code works for XLS and creates a RTL sheet:下面的代码适用于 XLS 并创建一个 RTL 表:

 exporter = new JRXlsExporter();
 exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
 SimpleXlsReportConfiguration xlsReportConfig = new SimpleXlsReportConfiguration();
 xlsReportConfig.setSheetDirection(RunDirectionEnum.RTL);
 exporter.setConfiguration(xlsReportConfig);    

However when I try the same code to make a XLSX file the sheet direction will not change to RTL:但是,当我尝试使用相同的代码制作 XLSX 文件时,工作表方向不会更改为 RTL:

exporter = new JRXlsxExporter();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsxReportConfiguration xlsxReportConfiguration =  new SimpleXlsxReportConfiguration();
xlsxReportConfiguration.setSheetDirection(RunDirectionEnum.RTL);
exporter.setConfiguration(xlsxReportConfiguration);

Seems to be a bug in the jasper report library tested with v 6.1.1 , adding code below after export it will work correctly (with poi libraries included in jasper report distribution, so no bug in POI...).似乎是使用v 6.1.1测试的jasper 报告库中的错误,导出后在下面添加代码它将正常工作(jasper 报告分发中包含 poi 库,因此 POI 中没有错误...)。

//out is the file after jasper report export
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(out));
int ns = workbook.getNumberOfSheets();
for (int i = 0; i < ns; i++) {
    XSSFSheet sheet = workbook.getSheetAt(i);
    sheet.setRightToLeft(true);
}
FileOutputStream outStream = new FileOutputStream(out);
workbook.write(outStream);
outStream.close();

Current bug report tracker on jaspersoft community jaspersoft 社区当前的错误报告跟踪器

I had a problem generating a report in XLSX, in the last version of JasperReports 6.1, but this code works for me:在 JasperReports 6.1 的最新版本中,我在 XLSX 中生成报告时遇到问题,但此代码对我有用:

Firstly, i configure the jasper print首先,我配置碧玉打印

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

After that, i configure the output for the generated report, in my case the report will be managed in memory by an ByteArrayOutputStream:之后,我为生成的报告配置输出,在我的情况下,报告将由 ByteArrayOutputStream 在内存中管理:

    ByteArrayOutputStream output = new ByteArrayOutputStream();

I create an instance of JRXlsxExporter for generate a file with the .xslx extension, and putting the printers and the output:我创建了一个 JRXlsxExporter 实例,用于生成一个扩展名为 .xslx 的文件,并放置打印机和输出:

    JRXlsxExporter exporter = new JRXlsxExporter();
    exporter.setExporterInput(SimpleExporterInput.getInstance(prints));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(output));

The next step is to create the report configuration for the exporter, when i put this code my report works!, so you must use it!:下一步是为导出器创建报告配置,当我把这段代码放在我的报告工作!,所以你必须使用它!:

    SimpleXlsxReportConfiguration xlsxReportConfiguration =  new SimpleXlsxReportConfiguration();
    xlsxReportConfiguration.setSheetDirection(RunDirectionEnum.RTL);
    exporter.setConfiguration(xlsxReportConfiguration);

Finally, generate the report and close the outputStream:最后,生成报告并关闭 outputStream:

    exporter.exportReport();
    output.flush();
    output.close();

I hope that this works for you我希望这对你有用

I finally used below code ( It is costy but works) , which is mentioned at https://community.jaspersoft.com/questions/523041/right-left-arabic-reports我最终使用了下面的代码(它很昂贵但有效),在https://community.jaspersoft.com/questions/523041/right-left-arabic-reports 中提到

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

Use it like:像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM