使用Java获取Excel信息
我需要一些帮助,因为我被卡住了 背景是这样的 我正在制作一个flex应用程序,其中一项服务是读取excel文件并将其放入数据库 我知道如何检索信息,但有一些细节让我卡住了 这里有:使用Java获取Excel信息,java,excel,apache-poi,Java,Excel,Apache Poi,我需要一些帮助,因为我被卡住了 背景是这样的 我正在制作一个flex应用程序,其中一项服务是读取excel文件并将其放入数据库 我知道如何检索信息,但有一些细节让我卡住了 这里有: 有些excel文件可能有1600行或更多行,但只有100行或200行有数据…但我想知道是否有类似cell.getlastrow函数但有数据,因为System.out.println(sheet.getLastRowNum())显然给了我1600或其他什么,但没有给我最后一行数据,也在我的插入验证中,我在我的条件下做了
System.out.println(sheet.getLastRowNum())
显然给了我1600或其他什么,但没有给我最后一行数据,也在我的插入验证中,我在我的条件下做了&(cell.getStringCellValue()!=null)
,但不起作用。。。我想停在最后一行,因为如果不是这样的话,我的代码会继续阅读,它会花很多时间去做一些不应该做的事情,然后插入一些我不想做的事情(工作簿eworkbook=WorkbookFactory.create(Archivo);
而不是新的XSSF或HSSF等的实例…使用WorkbookFactory,我是否安全,是xls还是xlsxSiveCuatro element;
ByteArrayInputStream Archivo = new ByteArrayInputStream( params.byteArray );
DsMgr myDB = new DsMgr();
Connection con = myDB.getConnection();
if (con != null){
Statement stmt;
try{
stmt = con.createStatement();
Workbook eworkbook = WorkbookFactory.create(Archivo);
Sheet sheet = eworkbook.getSheet("FORMATO SIVE 04");
Iterator<Row> rowIterator = sheet.iterator();
element = new SiveCuatro(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int NumRows = sheet.getPhysicalNumberOfRows();
int pasa = NumRows + 1;
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
if ( (cell.getColumnIndex() >= 0) && (pasa >= 1 ) ){
if(cell.getColumnIndex() == 1){element.setLaboratorio_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 2){element.setCaso_IdLab(cell.getStringCellValue());}
if(cell.getColumnIndex() == 3){element.setEstado_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 4){element.setMunicipio_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 5){element.setEnfermedad_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 6){element.setEspecie_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 10){element.setTipoMuestra_Id(cell.getStringCellValue());}
if(cell.getColumnIndex() == 18)element.setTecnica_Id(cell.getStringCellValue());
if(cell.getColumnIndex() == 19)element.setUsuario_Id(cell.getStringCellValue());
if(cell.getColumnIndex() == 21)element.setCaso_Fecha(cell.getStringCellValue());
if(cell.getColumnIndex() == 22)element.setCaso_Anio(cell.getStringCellValue());
if(cell.getColumnIndex() == 29)element.setPropietario(cell.getStringCellValue());
if(cell.getColumnIndex() == 30)element.setGranjaPredio(cell.getStringCellValue());
if(cell.getColumnIndex() == 31)element.setFuncionZoote(cell.getStringCellValue());
if((cell.getStringCellValue() != null)){ break;}
}
break;
case Cell.CELL_TYPE_NUMERIC:
if ( (cell.getColumnIndex() >= 0) && (pasa >= 1 ) ){
if(cell.getColumnIndex() == 0){element.setCaso_Mes(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 7){element.setPobAnimal_Total(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 8){element.setPobAnimal_Enfermos(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 9){element.setPobAnimal_Muertos(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 11){element.setTotal_Muestras(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 12){element.setRes_Positivos(cell.getNumericCellValue());}
if(cell.getColumnIndex() == 13)element.setRes_Negativos(cell.getNumericCellValue());
if(cell.getColumnIndex() == 14)element.setRes_Nt(cell.getNumericCellValue());
if(cell.getColumnIndex() == 15)element.setRes_Sospechoso(cell.getNumericCellValue());
if(cell.getColumnIndex() == 16)element.setCaso_Obs(cell.getNumericCellValue());
if(cell.getColumnIndex() == 17)element.setCaso_TipoCepa(cell.getNumericCellValue());
if(cell.getColumnIndex() == 20)element.setCaso_IPIC(cell.getNumericCellValue());
if(cell.getColumnIndex() == 23)element.setCaso_Estatus(cell.getNumericCellValue());
if(cell.getColumnIndex() == 24)element.setCaso_Id(cell.getNumericCellValue());
if(cell.getColumnIndex() == 25)element.setCuadrante_Id(cell.getNumericCellValue());
if(cell.getColumnIndex() == 26)element.setLocalidad_Id(cell.getNumericCellValue());
if(cell.getColumnIndex() == 27)element.setCaso_X(cell.getNumericCellValue());
if(cell.getColumnIndex() == 28)element.setCaso_Y(cell.getNumericCellValue());
}
break;
}
}
System.out.println(sheet.getLastRowNum());
sivecatro元件;
ByteArrayInputStream Archivo=新的ByteArrayInputStream(参数byteArray);
DsMgr myDB=新的DsMgr();
Connection con=myDB.getConnection();
如果(con!=null){
报表stmt;
试一试{
stmt=con.createStatement();
工作簿eworkbook=WorkbookFactory.create(Archivo);
Sheet Sheet=eworkbook.getSheet(“FORMATO SIVE 04”);
迭代器rowIterator=sheet.Iterator();
元素=新的SiveCuatro(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
int NumRows=sheet.getPhysicalNumberOfRows();
int pasa=NumRows+1;
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
if((cell.getColumnIndex()>=0)和&(pasa>=1)){
如果(cell.getColumnIndex()==1){element.setLaboratorio_Id(cell.getStringCellValue());}
如果(cell.getColumnIndex()==2){element.setCaso_IdLab(cell.getStringCellValue());}
如果(cell.getColumnIndex()==3){element.setEstado_Id(cell.getStringCellValue());}
如果(cell.getColumnIndex()==4){element.set市政_Id(cell.getStringCellValue());}
if(cell.getColumnIndex()==5){element.setEnfermedad_Id(cell.getStringCellValue());}
if(cell.getColumnIndex()==6){element.setspecie_Id(cell.getStringCellValue());}
if(cell.getColumnIndex()==10){element.setTipoMuestra_Id(cell.getStringCellValue());}
if(cell.getColumnIndex()==18)element.setTecnica_Id(cell.getStringCellValue());
if(cell.getColumnIndex()==19)element.setUsuario_Id(cell.getStringCellValue());
if(cell.getColumnIndex()==21)element.setcasou Fecha(cell.getStringCellValue());
if(cell.getColumnIndex()==22)element.setCaso_Anio(cell.getStringCellValue());
if(cell.getColumnIndex()==29)element.setPropietario(cell.getStringCellValue());
if(cell.getColumnIndex()==30)element.setGranjaPredio(cell.getStringCellValue());
if(cell.getColumnIndex()==31)element.setFunctionZoote(cell.getStringCellValue());
如果((cell.getStringCellValue()!=null)){break;}
}
打破
case Cell.Cell\u类型\u数值:
if((cell.getColumnIndex()>=0)和&(pasa>=1)){
如果(cell.getColumnIndex()==0){element.setCaso_ĀMes(cell.getNumericCellValue());}
如果(cell.getColumnIndex()==7){element.setPobAnimal_Total(cell.getNumericCellValue());}
if(cell.getColumnIndex()==8){element.setPobAnimal_Enfermos(cell.getNumericCellValue());}
if(cell.getColumnIndex()==9){element.setPobAnimal_Muertos(cell.getNumericCellValue());}
如果(cell.getColumnIndex()==11){element.setTotal_Muestras(cell.getNumericCellValue());}
如果(cell.getColumnIndex()==12){element.setRes_positives(cell.getNumericCellValue());}
if(cell.getColumnIndex()==13)element.setRes_negatios(cell.getNumericCellValue());
if(cell.getColumnIndex()==14)element.setRes\Nt(cell.getNumericCellValue());
if(cell.getColumnIndex()==15)element.setRes_Sospechoso(cell.getNumericCellValue());
if(cell.getColumnIndex()==16)element.setCaso_Obs(cell.getNumericCellValue());
if(cell.getColumnIndex()==17)element.setCaso_TipoCepa(cell.getNumericCellValue());
如果
int lastColumn=0;
for (int rowNum = 0; rowNum < rowEnd; rowNum++){
Row row=sheet1.getRow(rowNum);
//need to check the rows that it should not be null
if(row!=null)
lastColumn = row.getLastCellNum();
else
continue;
//check each cell for null or blank
for (int cn = 0; cn < lastColumn; cn++){
Cell cell = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if(cell == null){
break;
}
//Remaining code for non-blank cells
}
}
int j = 0;
boolean invalido = false;
while(rowIterator.hasNext()) {
invalido = false;
Row row = rowIterator.next();
j++;
if (j == 1)
row = rowIterator.next();
for (int k = 0; k < 32 ; k++){
if (row.getCell(k) == null || row.getCell(k).toString().replaceAll("\\s","").isEmpty()){
invalido = true;
break;
}
}
if (!invalido){
element.setCaso_Mes(row.getCell(0).toString());
element.setLaboratorio_Id(row.getCell(1).toString());
element.setCaso_IdLab(row.getCell(2).toString());
element.setEstado_Id(row.getCell(3).toString());
element.setMunicipio_Id(row.getCell(4).toString());
element.setEnfermedad_Id(row.getCell(5).toString());
element.setEspecie_Id(row.getCell(6).toString());
element.setPobAnimal_Total(row.getCell(7).toString());
element.setPobAnimal_Enfermos(row.getCell(8).toString());
element.setPobAnimal_Muertos(row.getCell(9).toString());
element.setTipoMuestra_Id(row.getCell(10).toString());
element.setTotal_Muestras(row.getCell(11).toString());
element.setRes_Positivos(row.getCell(12).toString());
element.setRes_Negativos(row.getCell(13).toString());
element.setRes_Nt(row.getCell(14).toString());
element.setRes_Sospechoso(row.getCell(15).toString());
element.setCaso_Obs(row.getCell(16).toString());
element.setCaso_TipoCepa(row.getCell(17).toString());
element.setTecnica_Id(row.getCell(18).toString());
element.setUsuario_Id(row.getCell(19).toString());
element.setCaso_IPIC(row.getCell(20).toString());
element.setCaso_Fecha(row.getCell(21).toString());
element.setCaso_Anio(row.getCell(22).toString());
element.setCaso_Estatus(row.getCell(23).toString());
element.setCaso_Id(row.getCell(24).toString());
element.setCuadrante_Id(row.getCell(25).toString());
element.setLocalidad_Id(row.getCell(26).toString());
element.setCaso_X(row.getCell(27).toString());
element.setCaso_Y(row.getCell(28).toString());
element.setPropietario(row.getCell(29).toString());
element.setGranjaPredio(row.getCell(30).toString());
element.setFuncionZoote(row.getCell(31).toString());
@SuppressWarnings("unused")
ResultSet rset = stmt.executeQuery("INSERT INTO " +