我在这里需要一些帮助,因为我坚持......获取信息的Excel与Java
好背景是这样的......
我想提出一个Flex应用程序的这一服务,以读取Excel文件,并把它变成一个数据库...
我知道如何检索信息,但也有一些细节让我卡住了...
这里有:
- 一些excel文件可能有1600或更多的行,但只有100或200有数据...但我想知道是否有像cell.getlastrow函数但数据,因为
System.out.println(sheet.getLastRowNum());
给了我明显的1600或任何它是,但没有给我最后一行的数据也在我的插入验证我在我的情况下做了&& (cell.getStringCellValue() != null)
但是没有工作......并且我想停在最后一行,因为如果没有,那么我的代码会继续阅读并且会花费很多时间的东西,它应该不做,并插入一些我不想要的东西。 :( - 此外,一些领域是这样的“7”,“0”我的意思是数字,但如果我把它们作为getStringCellValue,它不会把它们!如果我把它们作为getNumericCellValue它把它们但像“7.0”和类似的东西,我怎么能得到它作为字符串?我想他们作为字符串,因为在我的数据库中,我需要他们像整数不漂浮或双打
- 而另一个问题,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类,所有的变量字符串,并在Cell.CELL_TYPE_STRING只是一个案例,但不插入值,在excel就像数字一样,excel文件将始终将单元格视为“GENERAL”而不是文本,而不是数字。 而在此代码版本中,我更改了我的SiveCuatro类中的构造函数以匹配字段数字,它插入的却是float或double,我希望它们作为字符串,它也不会停在最后一行的信息中。
PS。对不起,我的英语不好。请大家帮帮忙。
WOOW,感谢您的时间,我现在测试您的第一个答案,但第二个,element.setCaso_Mes(cell.getNumericCellValue()的toString());} GAV e我“无法调用原始类型双重 toString(),如果我尝试cell.getCellValue()。toString(); “单元格”没有该属性 在此先感谢! – jompi
尝试'element.setCaso_Mes(cell.getCellValue()。toString());' – Sankumarsingh
做完了,但它说没有这样的属性:( – jompi