java - 使用 Java 获取 Excel 信息

标签 java excel apache-poi

我被困在这里需要一些帮助...

嗯,背景是这样的……

我正在制作一个 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”之类的东西,我怎样才能将它作为字符串获取?我希望它们作为字符串,因为在我的数据库中我需要它们像整数而不是 float 或 double
  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 文件将始终将单元格设置为“GENERAL”,而不是文本,而不是数字,就像 GENERAL 一样。 在这个代码版本中,我更改了我的 SiveCuatro 类中的构造函数以匹配数字字段,它确实插入但作为 float 或 double ,我希望它们作为字符串,而且它不会在最后一行包含信息。

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

最佳答案

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

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

关于java - 使用 Java 获取 Excel 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450152/

相关文章:

Java 8、流过滤器、反射、NoSuchMethodException

java - 套接字异常 : socket is closed

mysql - SQL查询最近记录

excel - 无法为特殊单元格上的 VLOOKUP 提供引用

java - 我怎样才能使用apachi POI java从Excel中获取不包括空白空间(空白行)的总行数?

java - 使用 java 中的 apache poi 将文本输入到 Doc 文件中的表格单元格

apache-poi - POI 3.17 在克隆工作表中创建单元格注释会产生不一致的 xlsx

java - 通过socket编程访问远程目录

java - 如何避免制作冗长的构造函数

linux - 如何在 Linux 下以 MS Office 格式从 IDE(以 HTML 格式)粘贴带有语法高亮的文本?