0

次のようなExcelスプレッドシートがあります。

+-----------------+--------+----------------------+
| Title           | Value  |  Average Price List  |
+-----------------+--------+----------------------+
|                 |        |                      |   
| Item 1          |   10   |        9.99          |
| Item 2          |   20   |        9.99          |
| Item 3          |   30   |        8.99          |
| Total Royalty A |        |                      |
|                 |        |                      |
+-----------------+--------+----------------------+
| Item 1          |   10   |        9.90          |
| Item 2          |   20   |        5.69          |
| Item 3          |   30   |        9.99          |
| Total Royalty B |        |                      |
|                 |        |                      |
+-----------------+--------+----------------------+

平均列の合計値を取得したい。ただし、「合計ロイヤリティ A」と​​「合計ロイヤリティ B 」を使用して合計を区切る必要があります。つまり、結果は次のようになります。

Total Royalty A = 10.99
Total Royalty B = 11.88

キーが「 Totalロイヤリティー B」から「 Totalロイヤリティー A 」である HashMap を使用しました。if ステートメントのコードに問題があります。

if(totalCell.getCellType() == Cell.CELL_TYPE_STRING)

問題は、タイトルの下に空/空白のセルがあることだと思います。空のセルをスキップするためにさまざまな手法を使用しようとしましたが、その行で問題が発生し続けます。この問題にアプローチする方法について誰かが私に何か提案をしてくれれば幸いです。

これは、上で説明したことを処理することになっているメソッドです。

 public HashMap monthlyAverageUnitsTotal (HSSFSheet sheet, int colAvgNum, int colTitle )
    {


        HashMap hm = new HashMap();


        double sum = 0.0;
        String key = null;


        for(Row row : sheet)
        {

            Cell saleAverageCell = row.getCell(colAvgNum);
            Cell totalCell = row.getCell(colTitle);

            if(totalCell.getCellType() == Cell.CELL_TYPE_STRING)
            {
               String totalCellValue = totalCell.getStringCellValue().toString().trim();

               if(totalCellValue.startsWith("Total Royalty"))
               {
                   key = totalCell.getStringCellValue().toString().trim();

               }
            }

            if(saleAverageCell.getCellType() == Cell.CELL_TYPE_NUMERIC)
            {
                double saleAverageCellValue = saleAverageCell.getNumericCellValue();
                sum += saleAverageCellValue;

                // put the element in the map
                hm.put(key, sum);

                // return the value of sum to 0
                sum = 0.0;


            }


        }


        return hm;
    }
4

1 に答える 1

1

大量のデータを含む Excel スプレッドシートを解析する機能要件がありました。セル値の問題を活用するために、セル値を文字列形式で返す関数を作成したので、文字列、数値、日付、ブール値、または空白のセルで問題が発生することはありません (数式の値は処理されません)。

//Cell celdaActual is the actual cell in a row in a sheet in the book, the class
//has Spanish names and comments and is big for a post, so I'm just posting
//this function
public String getCellValue() {
    String strValor = "";
    int intValor;
    double dblValor;
    SimpleDateFormat objSimpleDateFormat;
    if (celdaActual != null) {
        strValor = celdaActual.toString();
        switch (celdaActual.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                strValor = strValor.trim();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(celdaActual)) {
                    objSimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    strValor = objSimpleDateFormat
                        .format(celdaActual.getDateCellValue());
                } else {
                    dblValor = celdaActual.getNumericCellValue();
                    if (Math.floor(dblValor) == dblValor) {
                        intValor = (int)dblValor;
                        strValor = String.valueOf(intValor);
                    } else {
                        strValor = String.valueOf(dblValor);
                    }
                }
                break;
            case Cell.CELL_TYPE_BLANK:
                strValor = "";
                break;
            case Cell.CELL_TYPE_ERROR:
                strValor = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                strValor = String.valueOf(celdaActual.getBooleanCellValue());
                break;
        }
    }
    return strValor;
}

それがうまくいくことを願っています。

于 2012-05-30T21:45:10.493 に答える