2

行き詰まったので、ここで助けが必要です...

うーん背景はこれか…

私はフレックスアプリケーションを作成しています。これの1つのサービスは、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. もう 1 つの質問ですが、Excel ファイルは xls または xlsx である可能性がありWorkbook eworkbook = WorkbookFactory.create(Archivo);ます。新しい XSSF または HSSF などのインスタンスの代わりに作成する方がよいと読みました。

理解を深めるために、それを行うコードの一部を配置します...

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 に 1 つのケースしかありませんが、Excel で数値のような値を挿入しません。 Excelファイルには、セルが常に「GENERAL」としてテキストではなく、数値ではなく、常にGENERALとして含まれます。そして、このコードバージョンでは、SiveCuatro クラスのコンストラクターを数値フィールドに一致するように変更し、挿入しますが、float または double として挿入し、それらを String として使用したいのですが、情報の最後の行で停止しません。

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 のセル/行は異なります。実際には、空白の値を持つシートを見ると両方とも同じように見えますが、プログラム的には、セル/行を初期化していない場合、空白ではなくnullとして扱われます。その場合、(cell.getStringCellValue() != null)十分ではありません。これを回避するには、MissingCellPolicyを使用してすべての空白セルを null に変換し、null 値を確認する必要があります。また、ヌル行も確認する必要があります。たとえば、シートの合計行数が 10 であるが、5 行目が null の場合。Null は空白ではなく、UN で初期化された行を意味します。その場合Row row=sheet1.getRow(rowNum);、エラーは表示されませんが、表示される場合がrow.getLastCellNum();ありますnullPointerException. この問題を解決するには、最後の行番号を取得する前に、行が null であってはならないことを確認する必要があります。次のコードを確認してください

    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. あなたの質問「新しい XSSF や HSSF などのインスタンスの代わりに...WorkbookFactory では、xls と xlsx のどちらでも安全ですか?」....はい、これは安全です。

于 2013-08-26T19:12:11.683 に答える