javareaderjxls

How to add the row num to the employee loop variable?


I want to add the current row number of the excel row to the mapped Employee variable.

From the example below: I want to know that the employee "Yuri" is in excel row number 8.

But I can't find any way to access it. XLSRowCursor has it but how can I add it to the mapped bean? I know that the reader uses the current processing row number when writing an exceptions and POI has it too.

A simple self row counting solution on my side isn't a valid idea, because we use the skip row at error mechanism.

Any tips or hints?

The xml file:

 <?xml version="1.0" encoding="ISO-8859-1"?>
   <workbook>
     ....
    <loop startRow="7" endRow="7" items="department.staff" var="employee" varType="net.sf.jxls.reader.sample.Employee">
      <section startRow="7" endRow="7">
       <mapping row="7" col="0">employee.name</mapping>
       <mapping row="7" col="1">employee.age</mapping>
       <mapping row="7" col="3">employee.payment</mapping>
       <mapping row="7" col="4">employee.bonus</mapping>
     </section>
  ...
 </loop>
 </worksheet>
</workbook>

The excel file: Employees

6 Name  Age Birth Date  Payment Bonus   Total   Superior Name
7 Oleg  32  2-Jan-74    2000    20,00%  2400    Maxim
8 Yuri  29  26-Sep-77   1800    15,00%  2070    Oleg
9 Leonid    30  12-Feb-76   1700    20,00%  2040    Oleg
10 Alex 28  18-Aug-78   1600    20,00%  1920    Oleg
11 Employee Payment Totals:         7100        8430    

Solution

  • You can extend SimpleBlockReaderImpl and override its read(XLSRowCursor cursor, Map beans) method to use teh XLSRowCursor to get the current Excel row and inject it into the bean.

    Currently there is no automatic way to inject your own CustomBlockReader implementation via XML so you would have to do it manually by getting all the sheet readers from XLSReader and replacing inner block readers with your custom instances.