Estou utilizando o Apache POI para pegar dados de uma planilha Excel, porém uma das colunas em algumas linhas não possui valor (célula vazia).
package com.movie.controller;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.movie.model.Movie;
import lombok.Cleanup;
public class PlanilhaController {
public List<Movie> movieList() throws IOException, InvalidFormatException {
List<Movie> movies = new ArrayList<Movie>();
@Cleanup FileInputStream file = new FileInputStream("src/main/resources/static/movielist.xlsx");
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
@SuppressWarnings("unchecked")
List<Row> rows = (List<Row>) toList(sheet.iterator());
rows.remove(0);
rows.forEach(row -> {
@SuppressWarnings("unchecked")
List<Cell> cells = (List<Cell>) toList(row.cellIterator());
// Movie movie = Movie.builder()
// .ano((int) cells.get(0).getNumericCellValue())
// .titulo(cells.get(1).getStringCellValue())
// .estudio(cells.get(2).getStringCellValue())
// .produtores(cells.get(3).getStringCellValue())
// .vencedor(cells.get(4).getStringCellValue())
// .build();
//
// movies.add(movie);
System.out.println("Testeee! " + cells.size());
System.out.print("Célula vencedor: ");
System.out.println(cells.get(4) == null ? "" : cells.get(4).getStringCellValue());
});
return movies;
}
public List<?> toList(Iterator<?> iterator) {
return IteratorUtils.toList(iterator);
}
public void imprimir(List<Movie> movies) {
movies.forEach(System.out::print);
}
}
Exception:
Exception in thread “restartedMain” java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.get(ArrayList.java:427) at com.movie.controller.PlanilhaController.lambda$0(PlanilhaController.java:54) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at com.movie.controller.PlanilhaController.movieList(PlanilhaController.java:38) at com.movie.MovieApiApplication.main(MovieApiApplication.java:21) … 5 more