从Jasperstudio触发Java

时间:2016-05-05 06:39:35

标签: java jasper-reports

我有以下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();
}}}

1 个答案:

答案 0 :(得分:0)

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

反过来说,从.jasper报告中调用Java代码(根据我的理解情况)是不可能的,因为jasper文件已经是具有特定宽度的编译报告。