2016-06-22 6 views
0

ArrayList<String[]>構造体に列の値を挿入する必要があります。私は2つのデータ構造を使用しApache POIの値をArrayListに挿入する<String[]>

enter image description here

:私はこのExcelシートを持っている場合。列名はHashMap、値はArrayList<String[]>です。私は列の名前をちょうど良い得ることができますが、値を取得する方法を把握することはできません。ここで

は私のコードです:

Map<String, Integer> columnNames = new HashMap<String, Integer>(); 
List<String[]> values = new ArrayList<String[]>(); 
DataFormatter dataFormat = new DataFormatter(); 
FormulaEvaluator eval = workbook.getCreationHelper().createFormulaEvaluator(); 

Row row; 
int valueIndex = 0; 

for(int i = 0; i < sheet.getLastRowNum(); i++) { 
    row = (Row) sheet.getRow(i); 

    Iterator<Cell> cells = row.cellIterator(); 

    // Read each cell in the row 
    for(int j = 0; cells.hasNext(); j++) { 
     Cell cell = cells.next(); 

     // Column Headers 
     if(cell.getRowIndex() == 0) { 
      eval.evaluate(cell); 
      String cellVal = dataFormat.formatCellValue(cell, eval); 
      columnNames.put(cellVal, j); 

     // Column values 
     } else { 
      eval.evaluate(cell); 
      String cellVal = dataFormat.formatCellValue(cell, eval); 

      // What do I write here? 
      values.add(j, //cellVal_at_valueIndex); 
     } 
    } 

    // Go to next index for values (down 1 row) 
    valueIndex++; 
} 

私は、この行にArrayListに値を追加するかどうかはわかりません。

values.add(j, //cellVal_at_valueIndex); 

私はちょうどcellValを置く場合、それは相関しないだろうHashMapの列マッピングに正しく反映されます。 Bascially、私はそれが次のようになりたい:ここから

HashMap:      ArrayList: 
------------------   -------------------- 
Key  | Value   String | Index   
------------------   -------------------- 
id   0    1    0 
number  1    2    1 
time   2    ...   ... 

、私はそれぞれの列と任意の数の値が存在する可能性があるため、私はこのようにそれをやっている

values.get(columnNames.get("id"))[valueIndex];ような何かを行うことができます。また、getメソッドを使用して特定の列から値を取得する方が簡単になります。

文字列配列にどのように値を追加できますか?私は何かが簡単でないように感じる。前もって感謝します! Getting the cell contentsリンクを見てみましょう詳しくは

if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) 
{ 
    System.out.print(cell.getNumericCellValue()); 
    values.add(cell.getNumericCellValue()); 
} 
else if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
{ 
    System.out.print(cell.getRichStringCellValue()); 
    values.add(cell.getRichStringCellValue()); 
} 
else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
{ 
    System.out.print(cell.getBooleanCellValue()); 
    values.add(cell.getBooleanCellValue()); 
} 

答えて

関連する問題