Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove template row in JXLS when input list is empty

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?

like image 200
Steven Mackenzie Avatar asked Feb 23 '26 10:02

Steven Mackenzie


2 Answers

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.

like image 112
Serkan Özkan Avatar answered Feb 25 '26 00:02

Serkan Özkan


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.

like image 31
Yahia Avatar answered Feb 25 '26 01:02

Yahia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!