用Java获取Excel信息

我在这里需要一些帮助,因为我卡住了…

那么背景是这个…

我正在做一个灵活的应用程序,这是一个服务,阅读一个Excel文件,并将其放入一个数据库…

我知道如何检索信息,但有一些细节让我卡住了…

这里有:

  1. 一些Excel文件可能有像1600或更多的行,但只有100或200有数据…但我想知道是否有像cell.getlastrow函数但数据,因为System.out.println(sheet.getLastRowNum()); 给了我明显1600或任何它是,但没有给我的数据最后一行也在我的插入validation我没有&& (cell.getStringCellValue() != null)在我的情况,但没有工作…我想在最后一行停下来,因为如果没有,那么我的代码就会继续阅读,并且需要很多时间去做一些它不应该做的事情,并且插入一些我不想做的事情。 🙁
  2. 此外,一些领域是这样的“7”,“0”我的意思是数字,但如果我把它们作为getStringCellValue,它不会把它们! 如果我把它们作为getNumericCellValue它把它们,但像“7.0”和类似的东西,我怎么能得到它作为string? 我希望他们作为string,因为在我的数据库中,我需要他们像整数不漂浮或双打
  3. 还有一个问题,excel文件可能是xls或xlsx,我认为它更好地使Workbook eworkbook = WorkbookFactory.create(Archivo); 而不是新的XSSF或HSSF的实例…与WorkbookFactory,我会安全无论是xls或xlsx?

为了更好的理解,我会把一些代码放在我这样做的地方。

 SiveCuatro 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()); 

总结这个…这个代码的第一个版本,有我的SiveCuatro类,所有variables作为string,只有一个情况在Cell.CELL_TYPE_STRING,但不插入在Excel中的值是像数字, excel文件将始终将单元格视为“GENERAL”而不是“GENERAL”,而不是“GENERAL”。 而在这个代码版本中,我改变了我的SiveCuatro类中的构造函数来匹配字段数字,它插入,但作为浮动或双打,我想他们作为string,也不会停在最后一行的信息。

PS。 对不起,我的英语不好。 请大家在这里一点帮助。

  1. 你的情况似乎有一些概念上的问题。 空白单元格/行空单元格/行是不同的。 实际上,当您看到工作表的空白值时,两者看起来都是相同的,但是通过编程,如果您没有初始化任何单元格/行,它将被视为空值,而不是空白。 在这种情况下, (cell.getStringCellValue() != null)是不够的。 为了避免这种情况,您需要使用MissingCellPolicy来将所有空白单元格转换为空值,然后检查空值。 另外,您还需要检查空行。 例如,如果表格中的总行数是10,但第5行是空的。 Null表示不是空白的,而是UN初始化的行。 在这种情况下, Row row=sheet1.getRow(rowNum); 将不会显示任何错误,但row.getLastCellNum(); 可能会显示nullPointerException 。 为了解决这个问题,你需要在得到最后一个行号之前检查该行不应该为空。 请检查下面的一段代码

     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 } } 
  2. 要获取单元格值作为string,您可以使用cell.toString();

  3. 你的问题“而不是新的XSSF或HSSF等实例与WorkbookFactory,我会是安全的,无论是XLS或XLSX? ….是的,这将是安全的。

嘿,我刚刚弄明白,我改变了很多东西,但看起来像这样…

感谢Sankumarsingh为您的时间和帮助

 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 " + 

希望它可以帮助别人