【问题标题】:dynamic column width with jasper reportjasper 报告的动态列宽
【发布时间】:2020-07-07 20:16:40
【问题描述】:

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

我正在使用交叉表,因为我有动态列数。我将数据源作为 bean 的参数提供,所以我在生成报告之前就知道了许多列。我想要“动态”宽度的列。

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

直到现在我发现了如何加载文件并且我得到了正确的乐队(我知道它总是第二个)和正确的元素(动态交叉表)。我不知道如何设置列宽。

我当前的代码:

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

Tnx 提前寻求帮助。

【问题讨论】:

标签: java jasper-reports


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    相关资源
    最近更新 更多