You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Java List对象传入BIRT报表并生成Excel导出报表?

How to Iterate a Java List in BIRT and Export to Excel

Absolutely, you can pass your List of data objects as a single parameter to BIRT and iterate over it to build your Excel report. Let's walk through the exact steps to make this work smoothly:

1. Define a Java Object Parameter in BIRT

First, set up a parameter in your BIRT report to accept the List:

  • Open your .rptdesign file in BIRT Report Designer.
  • Navigate to the Parameters tab, click "New Parameter".
  • Name the parameter (e.g., dataList), set the Data Type to Java Object.
  • Under Java Class, enter the fully qualified class name of your List's generic type (like java.util.List<com.yourcompany.YourDataBean>).
  • Make sure the JAR containing your YourDataBean class is added to BIRT's classpath (Window > Preferences > Report Design > Classpath) so the designer can recognize the custom type.

2. Create a Scripted Data Source & Data Set

BIRT doesn't have a built-in data source for Java Lists, so we'll use a Scripted Data Source and Scripted Data Set to iterate over your data:

Step 2.1: Add a Scripted Data Source

  • In the Data Explorer panel, right-click "Data Sources" > New Data Source > Select "Scripted Data Source" > Name it (e.g., ListDataSource) > Finish.

Step 2.2: Add a Scripted Data Set

  • Right-click the new data source > New Data Set > Name it (e.g., ListDataSet) > Finish.
  • Switch to the Script tab in the data set editor:
    • Open Script: Initialize the List and index variable:
      // Fetch the List parameter from the report context
      globalList = reportContext.getParameterValue("dataList");
      currentRowIndex = 0;
      
    • Fetch Script: Iterate over the List and populate row data:
      if (currentRowIndex < globalList.size()) {
          // Get the current data bean from the List
          var dataBean = globalList.get(currentRowIndex);
          // Map bean properties to data set fields (match field names you define next)
          row["customerName"] = dataBean.getCustomerName();
          row["orderAmount"] = dataBean.getOrderAmount();
          // Add more fields based on your data structure
          currentRowIndex++;
          return true; // Signal there's more data to fetch
      } else {
          return false; // End of data iteration
      }
      
  • Go to the Result Set tab, click "New" to add fields that match the properties you mapped in the fetch script (e.g., customerName, orderAmount).

3. Design Your Report Layout

  • Drag a Table element onto the report canvas.
  • Bind the table to your ListDataSet: in the table's properties, go to Binding > Select your data set, then map the data set fields to table columns.
  • Customize headers, cell formatting, and styling to match your original HTML table's look.

4. Export the Report to Excel

BIRT natively supports Excel export. You have two options:

Option 1: Export from BIRT Designer

  • Run the report, click the "Export" button > Select "Excel" (choose .xls or .xlsx) > Save the file.

Option 2: Export via BIRT API (Server-Side)

If you're running the report from your backend code, use the BIRT engine API to export directly to Excel:

// Initialize BIRT Report Engine (setup code omitted for brevity)
IReportEngine reportEngine = ReportEngineFactory.createReportEngine();

// Load your report design
IReportRunnable reportDesign = reportEngine.openReportDesign("/path/to/your/report.rptdesign");

// Create run and render task
IRunAndRenderTask task = reportEngine.createRunAndRenderTask(reportDesign);

// Pass your List parameter to the report
task.setParameterValue("dataList", yourJavaListObject);

// Configure Excel export options
HTMLRenderOption renderOptions = new HTMLRenderOption();
renderOptions.setOutputFormat("xls"); // Use "xlsx" for the newer Excel format
renderOptions.setOutputFileName("/path/to/output/report.xls");

task.setRenderOption(renderOptions);
task.run();
task.close();

Key Tips to Avoid Issues

  • Ensure your data bean class has public getter methods for all properties you want to access in the script.
  • Double-check parameter names (they're case-sensitive) between your backend code and BIRT report.
  • If you hit classpath errors, confirm your data bean JAR is added both in the BIRT designer and your server's classpath.

内容的提问来源于stack exchange,提问作者vik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:37:27