使用java读取单列excel表 [英] Read a single column of excel sheet using java

查看:30
本文介绍了使用java读取单列excel表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张 Excel 表格.我想编写一个方法,将参数作为要读取的列号并返回一个包含该列中所有数据的数组.

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.

然后将该列元素放置在 xml 表中.我该如何编写方法来做到这一点?

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

推荐答案

使用 Apache POI.您可以在他们的使用页面中找到示例.

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

这篇关于使用java读取单列excel表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆