簡體   English   中英

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

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

我們使用的是 jasper 版本 6。我們可以導出到 EXCEL(XLS 和 XLSX)。

下面的代碼適用於 XLS 並創建一個 RTL 表:

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

似乎是使用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();

jaspersoft 社區當前的錯誤報告跟蹤器

在 JasperReports 6.1 的最新版本中,我在 XLSX 中生成報告時遇到問題,但此代碼對我有用:

首先,我配置碧玉打印

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

之后,我為生成的報告配置輸出,在我的情況下,報告將由 ByteArrayOutputStream 在內存中管理:

    ByteArrayOutputStream output = new ByteArrayOutputStream();

我創建了一個 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();

我希望這對你有用

我最終使用了下面的代碼(它很昂貴但有效),在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());
            }
        }
    }
}

像這樣使用它:

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