0

I'm using apache poi xssf to read excel files and I need to stop the reading when I find a row without field "date" (from the third row I have for each row a field date and if it is empty I have to stop with the file reading). So I have this class that implements SheetContentsHandler:

public class SheetToCSV implements SheetContentsHandler {

    //Custom comparator used to store key in map with the same order of excel cells
    private Comparator<String> excelOrder = comparingInt(String::length).thenComparing(String::compareTo);
    //data structure used to store cell coordinates and value where cell coordinates are the keys
    private TreeMap<String,String> rowValues=new TreeMap<String, String>(excelOrder);
    private int currentRow = -1;
    private AcquisitionForm acquisitionForm;
    private int sheetIndex;
    private DatabaseAcquisitionServices databaseAcquisitionServices;
    private int startRow;

    /**
     * @param acquisitionForm
     * @param sheetIndex
     * @param databaseAcquisitionServices
     */
    public SheetToCSV(AcquisitionForm acquisitionForm, int sheetIndex,
            DatabaseAcquisitionServices databaseAcquisitionServices, int startRow) {
        super();
        this.acquisitionForm = acquisitionForm;
        this.sheetIndex = sheetIndex;
        this.databaseAcquisitionServices = databaseAcquisitionServices;
        this.startRow=startRow;
    }

    public void startRow(int rowNum) {
        //add +1 to keep the same numeration of excel file, so start from 1 and not 0
        currentRow = rowNum + 1;
    }

    public void cell(String cellReference, String formattedValue,
            XSSFComment comment) {
        if (currentRow <= startRow) { 
            return; 
        }
        rowValues.put(cellReference, formattedValue);
    }

    @Override
    public void endRow(int rowNum) {
        if (!rowValues.isEmpty()){
            try {
                databaseAcquisitionServices.archiveAcquisition(new TreeMap<>(rowValues), currentRow, acquisitionForm, sheetIndex);
            } catch (ExcelReadException e) {
                return;
            }
            rowValues.clear();
        }
    }

    @Override
    public void headerFooter(String text, boolean isHeader, String tagName) {
    }
}

My method databaseAcquisitionServices.archiveAcquisition() throws an exception when it finds the first row without the date but I only catch this exception because the endRow method can't throw any exception. Part of archiveAcquisiont where the code launchs the exception (the whole code is too long):

if (actualRowValues.get(ExcelMappingCoordinate.date.getCoordinate()+index)==null){
    String message = "Threw exception in DatabaseServicesImpl::archiveAcquisition : the excel file contain no longer usuful row";
    LOG.error(message);
    throw new ExcelReadException(message);
}
try {
    date = format.parse(actualRowValues.get(ExcelMappingCoordinate.date.getCoordinate()+index));
    } catch (ParseException e) {
        LOG.error("Threw exception in DatabaseServicesImpl::archiveAcquisition : " + ErrorExceptionBuilder.buildErrorResponse(e));
}

With the actual code I only go to the next row but after the first null date row, all the followed row are equal. How can I exit from the reading? Many thanks

luca
  • 3,248
  • 10
  • 66
  • 145

0 Answers0