碧玉报告的动态列宽

dynamic column width with jasper report

我在 .jrxml 中有一个非常基本的模板,其中包含带有 header 和一个单元格的交叉表元素。

我正在使用交叉表,因为我有动态的列数。我将数据源作为来自 bean 的参数,因此我在报告生成之前就知道了一些列。我想要 "dynamic" 宽度的列。

我正在尝试读取现有的 jrxml,然后在生成报告之前根据列数修复列宽。

直到现在我才知道如何加载文件,并且我得到了正确的波段(我知道它总是第二个)和正确的元素(dynamicCrosstab)。我找不到如何设置列宽。

我当前的代码:

JasperDesign template = JRXmlLoader.load("C:\repos\templateFile.jrxml");
JRBand[] bands = template.getAllBands();
//getting crosstab
JRElement element = bands[1].getElementByKey("dynamicCrosstab");
if (element instanceof JRCrosstab) {
    //missing code to get cells and set width
}
//compiling template before using it to generate report
JasperCompileManager.compileReportToFile(template, "C:\repos\templateFile.jasper");

提前感谢任何帮助。

这不是一个很好的解决方案,但它确实有效。

    if (element instanceof JRDesignCrosstab) {
        //repair cells
        List<JRCrosstabCell> celice = ((JRDesignCrosstab) element).getCellsList();
        //repair content in cells
        ((JRDesignCrosstabCell) celice.get(0)).setWidth(newWidth);
        for (JRChild child : ((JRDesignCrosstabCell) celice.get(0)).getContents().getChildren()) {
            if(child instanceof JRDesignTextField){
                ((JRDesignTextField) child).setWidth(newWidth);
            }
        }
        //repair content in header
        List<JRCrosstabColumnGroup> headerji = ((JRDesignCrosstab) element).getColumnGroupsList();
        for (JRChild child : ((JRDesignCellContents) headerji.get(0).getHeader()).getChildren()) {
            ((JRDesignTextField) child).setWidth(newWidth);
        }
    }