1
Hello. I am working on a TCC in JAVA and one of its functions will be reading EXCEL files and sending the data to the Database.
I’m starting little by little in JAVA and some things still confuse a lot, which is why I’ve been doing a lot of research on this. The question is: As I do from the example below, I can start reading from line 5 of . xlsx and how to export this data to the Database?
I’m 3 days researching and since I found nothing specific, I’m resorting to Stackoverflow... Thanks from now on
public static void main(String[] args) {
FileInputStream fisPlanilha = null;
try {
File file = new File("C:\\Users\\wi-li\\Downloads\\PrograminhasLerXlsx\\xlsx\\planilhas\\planilhaDaAula.xlsx");
fisPlanilha = new FileInputStream(file);
//cria um workbook = planilha toda com todas as abas
XSSFWorkbook workbook = new XSSFWorkbook(fisPlanilha);
//recuperamos apenas a primeira aba ou primeira planilha
XSSFSheet sheet = workbook.getSheetAt(0);
//retorna todas as linhas da planilha 0 (aba 1)
Iterator<Row> rowIterator = sheet.iterator();
//varre todas as linhas da planilha 0
while (rowIterator.hasNext()) {
//recebe cada linha da planilha
Row row = rowIterator.next();
//pegamos todas as celulas desta linha
Iterator<Cell> cellIterator = row.iterator();
//varremos todas as celulas da linha atual
while (cellIterator.hasNext()) {
//criamos uma celula
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println("TIPO STRING: " + cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println("TIPO NUMERICO: " + cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println("TIPO FORMULA: " + cell.getCellFormula());
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LendoXLSX.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LendoXLSX.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fisPlanilha.close();
} catch (IOException ex) {
Logger.getLogger(LendoXLSX.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
If you can already read the file . xlsx with this code, to start reading from the 5th line is quite easy, you can simply call
rowIterator.next()
(that advances a line in iteration) 5 times in a row without taking what is returned. To send to a BD, it is also not difficult, but you will need a BD installed and know the commands to create tables and columns, insert data into cells, etc... recommend a beginner Java tutorial with BD.– Douglas
Great. I’ll poke around the code a little bit to implement the
rowIterator.next()
and then research more about the comic. Thank you.– Wilian Silva
whenever you call
rowIterator.next();
you advance in iteration, if you call this instruction 4 times before entering the loop, when entering the loop you will capture from the 5th line onwards. Search for "Iterators", the "Iterable" interface, or even the iterator project standard.– Douglas
Now I understood better... it was before the loop ' The more I move on this TCC, but my head goes into screw kkk Thank you very much Douglas. Now I’ll go deep into the database.
– Wilian Silva