Voltando para pedir socorro novamente com o TCC, dessa vez preciso enviar dados para o banco de dados a partir de uma tabela Excel. A “configuração” está ok, ou seja, está lendo a partir da linha correta e pega os dados direitinho, agora só enviar para o banco de dados… onde é aí que me perco.
package view;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
>
/**
* @author Wilian Nascimento da Silva <wilian.silva209@gmail.com>
*/
public class xlsxReader {
public String xlsxReader(String filename) {
FileInputStream fisPlanilha = null;
try {
File myfile = new File(filename);
FileReader filereader = new FileReader(myfile);
fisPlanilha = new FileInputStream(myfile);
//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();
//Gambiarra p pular as linhas da planilha
rowIterator.next();
rowIterator.next();
rowIterator.next();
rowIterator.next();
//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();
/*DEBUG*/
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());
}
/*DEBUG*/
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(xlsxReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(xlsxReader.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fisPlanilha.close();
} catch (IOException ex) {
Logger.getLogger(xlsxReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
}