简体   繁体   中英

jxls, Jasper Report or BIRT

I want to use a template base reporting framework like: jxls, Jasper Report or BIRT in a javaee application because it allows me to modify only the templates for satisfying customer requirements and without recompiling the java source code.

I use jxls, but I have a dynamic query and the number of output columns varies time to time so the database result set is transformed to:

List<Object[]>

. For generating excel report the following code added to the excel temple:

<jx:forEach items="${rows}" var="row">
    <jx:forEach items="${row}" var="cell">  ${cell} </jx:forEach>
</jx:forEach>

But If I have a list with more than 1000 elements it gives timeout exception.

What do you think? What is the problem? Is jxls a bad choice?

Regards

If you go with Jxls you should use Jxls-2 which was recently released. Jxls-2 has much better performance and much more flexible if you need to create dynamic columns. Check Dynamic Columns example in Jxls Demo project which shows an example of how to generate dynamic columns with Jxls-2 .

The example uses XML config to build the Jxls Commands but you can also construct them using Java API or Excel markup.

The XML config looks like this

<xls>
    <area ref="Template!A1:B4">
        <each items="headers" var="header" ref="Template!A3:A3" dir="RIGHT">
            <area ref="Template!A3:A3"/>
        </each>
        <each items="rows" var="row" ref="Template!A4:A4">
            <area ref="Template!A4:A4">
                <each items="row" var="cell" ref="Template!A4:A4" dir="RIGHT">
                    <area ref="Template!A4:A4"/>
                </each>
            </area>
        </each>
    </area>
</xls>

The template Excel file in this case is very simple and contains a markup only in 2 cells (for header and for data):

${header}

${cell}

The java code reads the template and builds XlsArea for processing

    InputStream is = DynamicColumnsDemo.class.getResourceAsStream(TEMPLATE);
    OutputStream os = new FileOutputStream(OUTPUT);
    Transformer transformer = TransformerFactory.createTransformer(is, os);
    InputStream configInputStream = DynamicColumnsDemo.class.getResourceAsStream(DYNAMIC_COLUMNS_DEMO_XML_CONFIG);
    AreaBuilder areaBuilder = new XmlAreaBuilder(configInputStream, transformer);
    List<Area> xlsAreaList = areaBuilder.build();
    Area xlsArea = xlsAreaList.get(0);

Next it sets the required data into the context and applies the transformation to the source area

    // creating context
    Context context = transformer.createInitialContext();
    context.putVar("headers", headers);
    context.putVar("rows", rows);
    // applying transformation
    logger.info("Applying area " + xlsArea.getAreaRef() + " at cell " + new CellRef("Result!A1"));
    xlsArea.applyAt(new CellRef("Result!A1"), context);

The result of the transformation is a table filled with dynamic data values for table headers, columns and rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM