1
I’m having trouble recording information in the database.
This information I extract from an Excel file.
For example, I have 3 fields and do not know how to take the first cell and record in the bank, then take the second and record and finally take the third and record.
Remembering that are various information that each column has.
My code is 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.
Could you give examples of column values? Depending on the case if possible, it is simpler to convert the spreadsheet to csv and import this file directly from the database.
– rray
Of course, username "Fernando" - fullname "Fernando Santos" - password "test"
– Fernando