How do I handle empty cells while reading Excel with org.jxls in Spring Boot?

147 Views Asked by At

I am using the org.jxls library in a Spring Boot project to read an Excel file. The reading process is working fine, but when it encounters an empty cell, it throws an org.jxls.reader.XLSDataReadException exception. I would like to skip reading the empty cell and continue with the next cell.

Here is my XML configuration file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<workbook>
    <worksheet name="Sheet1">
        <section startRow="0" endRow="0" />
        <loop startRow="1" endRow="1" items="estimations" var="estimation"
            varType="com.osi.exceljxls.entities.Estimation">
            <section startRow="1" endRow="1">
                <mapping row="1" col="0">estimation.role</mapping>
                <mapping row="1" col="1">estimation.noOfResources</mapping>
                <mapping row="1" col="3">estimation.projectLocation</mapping>
                <!-- Other mappings -->
                <!-- ... -->
                <cellcheck offset="0" evaluator="org.jxls.common.cell.reader.IsEmptyCellCheck" />
            </section>
            <loopbreakcondition>
                <rowcheck offset="0">
                    <cellcheck offset="0" />
                </rowcheck>
            </loopbreakcondition>
        </loop>
      </worksheet>
  </workbook>

I have already tried adding the <cellcheck /> tag as shown above, but it doesn't seem to be working.

I also considered handling the exception, but I am unsure how to proceed in that case.

I would appreciate any insights or suggestions on how to skip reading empty cells or how to handle the XLSDataReadException when an empty cell is encountered. Thank you in advance!

1

There are 1 best solutions below

0
Abhishek On

I have found the solution. In my service class, I need to add this line of code before reading the excel to avoid the org.jxls.reader.XLSDataReadException exception: ReaderConfig.getInstance().setSkipErrors(true);. With this adjustment, the code will read the empty cell and return 0 without any issues.