lh-masteryi

Excel最简洁解析

public static void main(String[] args) {

  File file = new File("D://123.xlsx");
  try {
    InputStream is = new FileInputStream(file);
    Workbook wb = null;
    if (file.getName().endsWith("xls")) { //Excel 2003
      try {
        wb = new HSSFWorkbook(is);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else if(file.getName().endsWith("xlsx")) { // Excel 2007/2010
      try {
        wb = new XSSFWorkbook(is);
      } catch (IOException e) {
        e.printStackTrace();
      }   
    }

    for (Sheet sheet : wb) {
      for (Row row : sheet) {
        StringBuffer sb = new StringBuffer();
        for (Cell cell : row) {
          sb.append(cell.toString());
        }
        System.out.println(sb);
      }
    }
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
}

发表于 2018-05-25 10:32  昭幻昊11  阅读(63)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章: