spring-projects / spring-batch-extensions

Spring Batch Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sheet index (1) is out of range (0..0)

flacohenao opened this issue · comments

Hey. I'm having this error using your extension library.

when I execute the job it works just fine. but at the second time of executing, it gives me that error.

 @Bean
    public PoiItemReader<ActivosExcel> excelReader() throws MalformedURLException {
        PoiItemReader<ActivosExcel> reader = new PoiItemReader<>();
        reader.setSaveState(false);
        reader.setLinesToSkip(1);
        reader.setResource(new UrlResource("file:\\file.xlsx") {
        });
        reader.setRowMapper(excelRowMapper());
        return reader;
    }

that is my Reader there... the complete error is:

Caused by: java.lang.IllegalArgumentException: Sheet index (1) is out of range (0..0)
	at org.apache.poi.xssf.usermodel.XSSFWorkbook.validateSheetIndex(XSSFWorkbook.java:1527) ~[poi-ooxml-3.16.jar:3.16]
	at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:1134) ~[poi-ooxml-3.16.jar:3.16]
	at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:121) ~[poi-ooxml-3.16.jar:3.16]
	at org.springframework.batch.item.excel.poi.PoiItemReader.getSheet(PoiItemReader.java:47) ~[spring-batch-excel-0.5.0-SNAPSHOT.jar:na]
	at org.springframework.batch.item.excel.AbstractExcelItemReader.openSheet(AbstractExcelItemReader.java:120) ~[spring-batch-excel-0.5.0-SNAPSHOT.jar:na]
	at org.springframework.batch.item.excel.AbstractExcelItemReader.doOpen(AbstractExcelItemReader.java:112) ~[spring-batch-excel-0.5.0-SNAPSHOT.jar:na]
	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
	... 79 common frames omitted

can you please help me? or should I do a custom Reader with POI. If that's the case, can you give me an example of how to do it?

thank you.

UPDATE: The first time that I use it, it works (my .xlsx file only have 1 sheet.) but the second time it doesnt because The Reader doesnt find a second sheet.. so it throws that error.

I did a little test and i just created another sheet in file and it worked. but still have the problem in code.

Where is the index incrementing? it should always be 0!

@simonbahuchet Thanks for the reply.

I solve the problem just creating the exact Reader and sheet just putting the .getIndexAt() method public and setting the sheet index I want to get.

I took a look at the library you recomended.. but its just for 2007 format, doesnt support .xlsx fortmat.

Do you think I managed that poorly?

thanks.

@flacohenao can you provide your code, where you have fixed above issue (Sheet index (1) is out of range (0..0)) @flacohenao

The PoiItemReader is holding some state, including the sheet. Your bean is a singleton so it will be reused. You want to define the reader as @StepScope or @JobScope so that you get a fresh instance each time you want to read a sheet.

That being said we can probably store the state in the ExecutionContext and retrieve it from there.