Read a single column of excel sheet using java

烈酒焚心 提交于 2019-12-13 15:11:26

问题


I have an excel sheet. I want to write a method which takes parameter as column number that to be read and return a array consist of all the data in that column.

And then to place that column element in xml sheet. How can I write a method to do that?


回答1:


Use Apache POI. You can find example in their usage page.

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();
        }
    }
}



回答2:


You may find this answer helpful: How to get columns from Excel files using Apache POI

That has all the code you need to extract all the numeric values from one column, whether they're a number cell or a formula cell containing a number result. I suspect you'll need something like that.



来源:https://stackoverflow.com/questions/5551815/read-a-single-column-of-excel-sheet-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!