如何将JRMapArrayDataSource添加到JasperDesign?

时间:2013-10-15 12:56:52

标签: java jasper-reports datasource

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

我的问题是如何在运行时将此dataSource添加到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;
}

1 个答案:

答案 0 :(得分: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);
}