I'm having trouble writing information to the database.
This information I extract from an Excel file.
For example, I have 3 fields and I do not know how to get the 1st cell and save it to the bank, then get the 2nd and save and finally get the 3rd and save.
Remembering that there is a lot of information that each column has.
My code looks like this:
public void readingSheet(String path) {
try {
File file = new File(path);
String name = file.toString();
int pos = name.lastIndexOf('.');
String ext = name.substring(pos + 1);
FileInputStream fileIn = new FileInputStream(file);
Workbook obj = null;
if (ext.equals("xlsx")) {
try {
//Metodo aceita o path do arquivo
obj = new XSSFWorkbook(fileIn);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
else if (ext.equals("xls")) {
try {
//Metodo nao aceita string do path do arquivo
obj = new HSSFWorkbook(fileIn);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
else {
throw new IllegalArgumentException("Arquivo recebido não é uma extensão do Excel");
}
int o = 0;
Sheet worksheet = obj.getSheet("Plan1");
Row row;
Cell cell;
for(int i = 1; i<= worksheet.getLastRowNum(); i++){
row = worksheet.getRow(i);
String linha = "";
for(int j = 0; j < 3; j++) {
cell = row.getCell(j);
if(cell.getCellType()==1) {
linha += " | " + cell.getStringCellValue();
}
else {
double aux = 0;
int aux2 = 0;
aux = cell.getNumericCellValue();
aux2 = (int) aux;
linha += " | " + aux2;
}
}
System.out.println("");
System.out.println("Linha: " + linha);
System.out.println("");
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Thank you all.