2

我在这里需要一些帮助,因为我被困住了......

嗯,背景是这样的……

我正在制作一个 flex 应用程序,其中一项服务是读取一个 excel 文件并将其放入数据库中......

我确实知道如何检索信息,但有一些细节让我陷入困境......

这里有:

  1. 一些 excel 文件可能有 1600 行或更多行,但只有 100 或 200 行有数据......但我想知道是否有像 cell.getlastrow 函数但有数据,因为System.out.println(sheet.getLastRowNum());显然给了我 1600 或其他任何东西,但没有给出我的最后一行数据也在我的插入验证中我 && (cell.getStringCellValue() != null) 在我的情况下做了但没有工作......我想停在最后一行因为如果不是那么我的代码会继续阅读并且需要很多时间它不应该做并插入我不想做的事情。:(
  2. 另外,有些字段是这样的“7”,“0”我的意思是数字但是如果我把它们作为getStringCellValue,它不会把它们!如果我将它们作为 getNumericCellValue 放置它们,但就像“7.0”之类的东西,我怎样才能将它作为字符串?我希望它们作为字符串,因为在我的数据库中我需要它们,比如整数而不是浮点数或双精度数
  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 类,所有变量都是字符串,只有一个案例在 Cell.CELL_TYPE_STRING,但不插入在 excel 中类似于数字的值, excel文件将始终将单元格作为“通用”而不是文本,而不是数字,就像通用一样。在这个代码版本中,我更改了我的 SiveCuatro 类中的构造函数以匹配数字字段,它确实插入但作为浮点数或双精度数,我希望它们作为字符串,它也不会在最后一行停止信息。

PS。对不起,我的英语不好。请大家帮忙。

4

2 回答 2

0

嘿,我刚刚发现我改变了很多东西......但看起来像这样......

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

希望它可以帮助某人

于 2013-08-26T23:19:11.583 回答
0
  1. 您的情况似乎存在一些概念问题。空白单元格/行和空单元格/行是不同的。实际上,当您看到带有空白值的工作表时,两者看起来都一样,但是以编程方式,如果您没有初始化任何单元格/行,它将被视为 null,而不是空白。在那种情况下,(cell.getStringCellValue() != null)是不够的。为避免这种情况,您需要为此使用MissingCellPolicy将所有空白单元格转换为 null,然后需要检查 null 值。此外,您还需要检查空行。例如,如果工作表中的总行数为 10,但第 5 行为空。Null 表示不是空白,而是未初始化的行。在这种情况下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. 要将单元格值作为字符串获取,您可以使用cell.toString();

  3. 您的问题“而不是使用 WorkbookFactory 的新 XSSF 或 HSSF 等实例,无论是 xls 还是 xlsx,我都会安全吗?” ....是的,这将是安全的。

于 2013-08-26T19:12:11.683 回答