2011-04-05 41 views
3

我有一个excel表格。我想编写一个方法,它将参数作为要读取的列号,并返回包含该列中所有数据的数组。使用java阅读excel表格的单列

然后将该列元素放在xml表中。我怎么写一个方法来做到这一点?

回答

7

使用Apache POI。你可以在他们的使用页面找到例子。

Sheet sheet1 = wb.getSheetAt(0); 
for (Row row : sheet1) { 
    for (Cell cell : row) { 
    // Here you might have to use the cell number to limit what you want. 
     CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); 
     System.out.print(cellRef.formatAsString()); 
     System.out.print(" - "); 

     switch(cell.getCellType()) { 
     case Cell.CELL_TYPE_STRING: 
     System.out.println(cell.getRichStringCellValue().getString()); 
     break; 
     case Cell.CELL_TYPE_NUMERIC: 
     if(DateUtil.isCellDateFormatted(cell)) { 
      System.out.println(cell.getDateCellValue()); 
     } else { 
      System.out.println(cell.getNumericCellValue()); 
     } 
     break; 
     case Cell.CELL_TYPE_BOOLEAN: 
     System.out.println(cell.getBooleanCellValue()); 
     break; 
     case Cell.CELL_TYPE_FORMULA: 
     System.out.println(cell.getCellFormula()); 
     break; 
     default: 
     System.out.println(); 
     } 
    } 
} 
+1

当然POI是关于从Java读取excel电子表格的方式。 – 2011-04-05 12:37:59