0

我已将报告创建为 JasperDesign(而不是 jrxml 文件)。此报告具有创建的正确字段(使用jasperDesign.addField)和正确的标题带(使用 创建jasperDesign.setColumnHeader() )。我没有使用与数据库的连接,而是创建了一个包含许多字段的 JRMapArrayDataSource。

我的问题是如何在运行时将此数据源添加到 JasperDesign?

public static void doPrintWithJasperReport(PlanTableModel tableModel) throws JRException
{
    long start = System.currentTimeMillis();
    JasperDesign jasperDesign = getJasperDesign(tableModel);

    String compiledSource = "/build/reports/NoXmlDesignReport.jasper";
    JasperCompileManager.compileReportToFile(jasperDesign, compiledSource);
    System.err.println("Compile time : " + (System.currentTimeMillis() - start));
    JasperDesignViewer viewer = new JasperDesignViewer(compiledSource,false);

}

public static JasperDesign getJasperDesign(PlanTableModel tableModel) throws JRException
{
    //JasperDesign
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("The dynamically generated report");
    jasperDesign.setPageWidth(842);
    jasperDesign.setPageHeight(595);
    jasperDesign.setOrientation(JRReport.ORIENTATION_LANDSCAPE);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(50);
    jasperDesign.setBottomMargin(50);

    //data source
    JRDataSource dataSource = createReportDataSource( tableModel);
    // I don't know how to add it to jasperDesign

    // create Detail band
    JRDesignBand band = new JRDesignBand();
    band.setHeight(240);

    // add Fields
    List<Map<String,String>> maps = createDataSourceMap(tableModel);
    Map<String,String> headerMap = maps.get(0);

    for(Map.Entry<String,String> entry : headerMap.entrySet())
    {
        JRDesignField field = new JRDesignField();
        field.setName(entry.getKey().replaceAll("\n", " "));
        field.setValueClass(String.class);
        jasperDesign.addField(field);

        //creating field on the detail band
        JRDesignTextField textField = new JRDesignTextField();
        textField.setX(60);
        textField.setY(0);
        textField.setWidth(200);
        textField.setHeight(20);
        textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
        JRDesignExpression expression = new JRDesignExpression();
        expression.setValueClass(String.class);
        expression.setText("$F{".concat(entry.getKey().replaceAll("\n", " ")).concat("}"));
        textField.setExpression(expression);
        textField.getLineBox().getTopPen().setLineWidth(1);
        textField.getLineBox().getRightPen().setLineWidth(1);
        textField.getLineBox().setLeftPadding(10);

        band.addElement(textField);

    }

    // add band
    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

    return jasperDesign;
}

private static JRDataSource createReportDataSource(PlanTableModel tableModel)
{
    JRMapArrayDataSource dataSource;
    Map[] reportRows = initializeMapArray(tableModel);
    dataSource = new JRMapArrayDataSource(reportRows);
    return dataSource;
}

public static Map[] initializeMapArray(PlanTableModel tableModel)
{
    HashMap[] reportRows = new HashMap[tableModel.getRowCount()];
    int hashMapRow = 0;

    // prepare data source


    return reportRows;
}
4

1 回答 1

0

我只是想出来如下,它可以帮助别人:

public static void doPrintWithJasperReport(TableModel tableModel) throws JRException
{
    dataSource = createReportDataSource(tableModel);
    JasperDesign jasperDesign = getJasperDesign(tableModel);

    String jasperCompiledSourcePath = "C:/NoXmlDesignReport.jasper";
    File file = new File(jasperCompiledSourcePath);
    if(file.exists())
        file.createNewFile()

    JasperCompileManager.compileReportToFile(jasperDesign, jasperCompiledSourcePath);
    JRReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    Map<String, Object> parameters = retrieveParameter();
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperCompiledSourcePath,parameters,dataSource);
    JRViewer jasperViewer = new JRViewer(jasperPrint);
    JFrame frame = new JFrame();
    frame.setSize(dimension);
    frame.setTitle("Second frame");
    frame.setContentPane(jasperViewer);
    frame.setAlwaysOnTop(true);
    frame.setVisible(true);
}
于 2013-10-21T09:06:53.707 回答