Remove template row in JXLS when input list is emp

2019-05-15 01:45发布

问题:

I'm using JXLS to populate a template excel spreadsheet. Sometimes, one input - a list - is empty. When this happens, the template fields which use this list are shown in the populated spreadsheet in full JXLS regalia - eg, "${someList.someValue}".

The fields in question are all in a single row (expanded to list.size() rows by JXLS). I want to clear the whole row when there are no records in the list.

Is there a way to do so in JXLS? Alternatively, can anyone suggest a complementary way to use JExcel or POI to do so?

回答1:

You can use <jx:if> tags in your excel template. For example if you have a template similar to the following

Row 1: <jx:if test="${!empty someData}">
Row 2: place holders for data that will be printed in the loop 
Row 3: </jx:if>

If someData is not empty then rows will be printed in the output but if someData is empty then no rows will be printed by this template.

I hope I could make it clear. Please see http://jxls.sourceforge.net/reference/tags.html for more information about tags.



回答2:

I think this is possible via POI - perhaps even POI object interface of JXLS:

From http://poi.apache.org/spreadsheet/how-to.html:
Modifying the file you have read in is simple. You retrieve the object via an assessor method, remove it via a parent object's remove method (sheet.removeRow(hssfrow)) and create objects just as you would if creating a new xls. When you are done modifying cells just call workbook.write(outputstream) just as you did above.

From http://jxls.sourceforge.net/reference/customprocessor.html:
With Row object you have access to related POI objects to modify cell style if required.



回答3:

Another reason the rows do not display based on the jx:if condition is if there is some space before the jx:if tag