I need to store the information of an xls file, in an arraylist, getting its columns one by one, I currently get all the file information, but I have no idea how to save each row in an arraylist, actually this is what I have
xls file format
rut carrera_id Seccion Fecha_I
11 14460-03 DEJ4501-001 12-10-2017
11 14460-03 DEJ4501-001 19-10-2017
22 14460-03 DEJ4501-001 12-10-2017
22 14460-03 DEJ4501-001 19-10-2017
22 14460-03 DEJ4501-001 26-10-2017
23 14460-03 DEJ4501-001 12-10-2017
23 14460-03 DEJ4501-001 19-10-2017
24 14460-03 DEJ4501-001 12-10-2017
25 14460-03 DEJ4501-001 12-10-2017
25 14460-03 DEJ4501-001 19-10-2017
25 14460-03 DEJ4501-001 26-10-2017
25 14460-03 DEJ4501-001 15-10-2017
class that formats for the arraylist
public class Formato {
String rut, carrera, seccion, fecha;
public Formato() {
}
public Formato(String rut, String carrera, String seccion, String fecha) {
this.rut = rut;
this.carrera = carrera;
this.seccion = seccion;
this.fecha = fecha;
}
public String getRut() {
return rut;
}
public void setRut(String rut) {
this.rut = rut;
}
public String getCarrera() {
return carrera;
}
public void setCarrera(String carrera) {
this.carrera = carrera;
}
public String getSeccion() {
return seccion;
}
public void setSeccion(String seccion) {
this.seccion = seccion;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
method where I read the xls file, and where I should save each row in the arraylist
public static void main(String[] args) {
try{
FileInputStream file= new FileInputStream(new File("C:\Users\javie\Desktop\ReporteInasistencias2017_2.xls"));
HSSFWorkbook wb= new HSSFWorkbook(file);
HSSFSheet sheet=wb.getSheetAt(0);
ArrayList<Formato> storage=new ArrayList();
FormulaEvaluator eva=wb.getCreationHelper().createFormulaEvaluator();
for(Row row: sheet){
for(Cell cell:row){
switch(eva.evaluateInCell(cell).getCellType()){
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue()+"\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue()+"\t\t");
break;
}
}
System.out.println();
}
}catch(FileNotFoundException e){
System.out.println("ruta incorrecta");
}catch(IOException e){
System.out.println("error al leer el archivo");
}
I need to keep every row in the arraylist storage, I hope you can help me
Update
I currently manage to save all the columns, but with something not very orthodox, I need you to help me improve the code, I currently have this:
public static void main(String[] args) {
try{
FileInputStream file= new FileInputStream(new File("C:\Users\javie\Desktop\ReporteInasistencias2017_2.xls"));
HSSFWorkbook wb= new HSSFWorkbook(file);
HSSFSheet sheet=wb.getSheetAt(0);
FormulaEvaluator eva=wb.getCreationHelper().createFormulaEvaluator();
ArrayList<String> rut=new ArrayList();
ArrayList<String> carrera=new ArrayList();
ArrayList<String> seccion=new ArrayList();
ArrayList<String> fecha=new ArrayList();
ArrayList<Formato> excelArray=new ArrayList();
for (int j=0; j< sheet.getLastRowNum()+1; j++) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(0);
rut.add(cell.toString());
}
for (int j=0; j< sheet.getLastRowNum()+1; j++) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(1);
carrera.add(cell.toString());
}
for (int j=0; j< sheet.getLastRowNum()+1; j++) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(2);
seccion.add(cell.toString());
}
for (int j=0; j< sheet.getLastRowNum()+1; j++) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(3);
fecha.add(cell.toString());
}
for(int i=1;i<rut.size();i++){
Formato ft=new Formato();
ft.setRut(rut.get(i));
ft.setCarrera(carrera.get(i));
ft.setSeccion(seccion.get(i));
ft.setFecha(fecha.get(i));
excelArray.add(ft);
System.out.println("rut del alummno: "+ft.getRut()+", carrera del alummno: "+ft.getCarrera()+", seccion del Alummno: "+ft.getSeccion()+", fecha de inasistencia: "+ft.getFecha());
}
System.out.println("arreglo: "+excelArray.size());
}catch(FileNotFoundException e){
}catch(IOException e){
}
}
currently everything works fine, but I want to optimize all those for