0

JavaプログラムからExcelシートを読み取るプログラムがあります。

以下のようにセルを反復処理しています。

Iterator cells = row.cellIterator();
String temp;
StringBuilder sb;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

while (cells.hasNext()) {
Cell cell = (Cell) cells.next();
temp = null;

    switch (cell.getCellType()) {
         case Cell.CELL_TYPE_STRING:
         temp = cell.getRichStringCellValue().getString();
         break;

    case Cell.CELL_TYPE_NUMERIC:

    if (DateUtil.isCellDateFormatted(cell)) {
        temp = sdf.format(cell.getDateCellValue());
       } else {
             temp = df.format(new BigDecimal(cell.getNumericCellValue()));
       }
          break;
          default:
     }
    if (temp == null || temp.equalsIgnoreCase("null")) {
        sb.append("").append(";");
    } else {
        sb.append(temp).append(";");
    }
 }

ご覧のとおり、エクセル行の値をセミコロンで区切って含む文字列ビルダーを作成しようとしています。

問題は、列の値が空の場合、2 つの連続したセミコロンを持つ文字列ビルダーで空の値として使用することです。

ただし、呼び出し

セル cell = (セル) cells.next();

空のセルを単に無視し、次の空でないセルにジャンプします。

だからライン

if (temp == null || temp.equalsIgnoreCase("null"))

満たされることはありません。

iterator で空の列の値のハンドルを取得する方法は?

4

3 に答える 3

3

これは実質的にこの質問の複製であるため、その質問に対する私の答えは基本的にあなたにも当てはまります。

Cell Iterator は、ファイルで定義されているセルのみを反復処理します。セルが Excel で一度も使用されていない場合、おそらくファイルに表示されないため (Excel は常に一貫しているわけではありません...)、POI には表示されません。

すべてのセルにヒットしたことを確認したい場合は、代わりにインデックスで検索し、null セルをチェックするか (セルがファイルに存在していないことを示します)、MissingCellPolicynull セルと空白セルをどのようにするかを制御するように設定する必要があります。処理された

したがって、本当にすべてのセルを取得したい場合は、次のようにします。

Row r = sheet.getRow(myRowNum);
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

for (int cn=0; cn<lastColumn; cn++) {
   Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
   if (c == null) {
      // The spreadsheet is empty in this cell
   } else {
      // Do something useful with the cell's contents
   }
}
于 2012-11-01T12:10:09.180 に答える
0

あなたはこれを行うことができます

int previous=0;
while(cell.hasNext())
{
    Cell cell = (Cell) cells.next();
    int current=cell.getColumnIndex();
    int numberofsemicolons=previous-current;
    for(numberofsemicolons)
    {
    sb.append("").append(";");
    }
    previous=current;
}

またはあなたができる

int numberofcells=row.getFirstCellNum()-row.getLastCellNum();
for(i=0;i<=numberofcells;i++)
{
     Cell cell = (Cell) cells.next();
     int current=cell.getColumnIndex();
     while(i<current)
     {
        sb.append("").append(";");
        i++
     }
}
于 2012-11-01T06:21:50.257 に答える