Zaw Than oo
Zaw Than oo

Reputation: 9935

How to print a table with List<List<String>> in JasperReports?

Let's say, I have fixed three columns (Name, Address, Email). But, I have to accept the parameter for the data as List<List<String>>. I don't know to use in jr:table.

I can also solve this case using a DTO class as below.

SomeDTO.java

public class SomeDTO {
    private String name;
    private String address;
    private String email;
    //getter setter
}

Transform List<List<String>> to List<SomeDTO> and use as new JRBeanCollectionDataSource(someDTOList). It is not my expected solution.

Parameter

List<String> list_1 = new ArrayList<String>();
list_1.add("AAA");
list_1.add("AAA_Address");
list_1.add("AAA_Email");
List<String> list_2 = new ArrayList<String>();
list_2.add("BBB");
list_2.add("BBB");
list_2.add("BBB_Email");
List<List<String>> rowList = new ArrayList<List<String>>();
rowList.add(list_1);
rowList.add(list_2);

Program

public void generate(String filePath, List<List<String>> rowDataList) {
    ....

    Map paramMap = new HashMap();
    paramMap.put("TableDataSource", new JRBeanCollectionDataSource(rowDataList));           
    JasperPrint print = JasperFillManager.fillReport(report, paramMap);
    JasperExportManager.exportReportToPdfFile(print, filePath);
}

Template

.....
<subDataset name="dynamicDataSource">
    <field name="paramList" class="java.util.List"/>
</subDataset>
<parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
....

<jr:table ....>
    <datasetRun subDataset="dynamicDataSource">
        <dataSourceExpression><![CDATA[$P{TableDataSource}]]></dataSourceExpression>
    </datasetRun>
    <jr:columnHeader...>
        ....
    </jr:columnHeader>
    <jr:detailCell ...>
        ...
        <textFieldExpression><![CDATA[$F{xxxxxx}]]></textFieldExpression> <-- Here, how can I print it out?
    </jr:detailCell>
</jr:table>     

Upvotes: 3

Views: 5165

Answers (1)

Darshan Lila
Darshan Lila

Reputation: 5868

You would require to implement a custom data source in order to fetch data from List<List <String>>. Here's how your datasource implementation should look like

import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class RowDataList implements JRDataSource {

    List<List<String>> superList;
    int index=-1;
    Object o;
    public RowDataList(List<List<String>> list) {
    // pass your list here

        superList=list;
    }

    public Object getFieldValue(JRField arg0) throws JRException {

        List<String> stringList=superList.get(index);
        o=stringList;
        return o;
    }

    public boolean next() throws JRException {

        index++;
        return index<superList.size();
    }
}

Now you can fill your report as follows:

public void generate(String filePath, List<List<String>> rowDataList) { 

    Map paramMap = new HashMap();    
    //put params as per your requirement     
    JasperPrint print = JasperFillManager.fillReport(report, paramMap,new RowDataList(rowDataList)); // note here the custom data source is utilized
    JasperExportManager.exportReportToPdfFile(print, filePath);

}

And after all you will have to make subtle changes to your .jrxml file. Here's how that would look.

.....
<subDataset name="dynamicDataSource">
    <field name="paramList" class="java.util.List"/>
</subDataset>
<field name="rowDataList" class="java.lang.ArrayList"/>
....

<jr:table ....>
    <datasetRun subDataset="dynamicDataSource">
        <dataSourceExpression><![CDATA[$F{rowDataList}]]></dataSourceExpression>
    </datasetRun>
    <jr:columnHeader...>
        ....
    </jr:columnHeader>
    <jr:detailCell ...>
        ...
        <textFieldExpression><![CDATA[$F{xxxxxx}]]></textFieldExpression> <-- Here, how can I print it out?
    </jr:detailCell>
</jr:table>

And you are ready to go.

Hope this helps.

Upvotes: 2

Related Questions