我有以下Java代码,它将通过修改JRXML来修改矩形的宽度。但是,报表运行时如何调用Java代码? Scriptlet并没有太大帮助。有没有一种方法可以在Studio中调用Java / jar文件来动态更改文本对象的宽度?

public class widthAdjust {

public static void main(String[] args) {
    JasperDesign design;
    try {
        design = JRXmlLoader.load("C:\\GanttChart_Java.jrxml");
    System.out.println(design);
    JRField[] fields = design.getFields();
    JRVariable[] variables =  design.getVariables();

    int newWidth = 100;
    int newX = 100;
    JRDesignBand baseBand = (JRDesignBand) design.getSummary();
    JRElement[] elements = baseBand.getElements();
    System.out.println(elements);
    for (int counter = 0; counter < elements.length; counter++) {
        JRDesignElement element = (JRDesignElement) elements[counter];
            element.setWidth(newWidth);
            element.setX(newX);
    }
    JasperCompileManager.compileReportToFile(design,"C:\\GanttChart_Java.jasper");
    JasperCompileManager.writeReportToXmlFile("C:\\GanttChart_Java.jasper","C:\\GanttChart_Java.jrxml");
    JasperCompileManager.compileReport("C:\\GanttChart_Java.jrxml");
} catch (JRException e) {
    e.printStackTrace();
}}}

最佳答案

要更改元素的属性,最好的方法是运行Java代码并使用Jasper API。

另一种方法是,无法从.jasper-report(据我所知的情况)中调用Java代码,因为jasper文件已经是具有特定宽度的已编译报告。

10-08 00:27