zoukankan      html  css  js  c++  java
  • java利用poi解析excel文件

    首先需要引入以下jar包

    如果使用maven,需要添加两个依赖

      <dependencies>
        <dependency>
              <groupId>org.apache.poi</groupId>
              <artifactId>poi</artifactId>
              <version>3.15</version>
          </dependency>
          <dependency>
              <groupId>org.apache.poi</groupId>
              <artifactId>poi-ooxml</artifactId>
              <version>3.15-beta2</version>
          </dependency>
      </dependencies>

    excel分为两种后缀,一是xls,二是xlsx。

    首先针对xls格式的excel进行解析:

        public List<String[]> readExcel(File file){
            List<String[]> list = new ArrayList<>();
            
            try {
                HSSFWorkbook hw = new HSSFWorkbook(new FileInputStream(file));
                
                for (int i = 0; i < hw.getNumberOfSheets(); i++) {
                    HSSFSheet hs = hw.getSheetAt(i);
                    if(hs.getLastRowNum() == 0)
                        continue;
                    
                    for (int j = 0; j <= hs.getLastRowNum(); j++) {//
                        HSSFRow row = hs.getRow(j);
                        String[] array = new String[row.getLastCellNum()];
                        
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            HSSFCell cell = row.getCell(k);
                            array[k] = this.getValue(cell);
                        }
                        list.add(array);
                    }
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            return list;
        }
        
        private String getValue(HSSFCell cell){
            String value;
            CellType type = cell.getCellTypeEnum();
            
            switch (type) {
            case STRING:
                value = cell.getStringCellValue();
                break;
            case BLANK:
                value = "";
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue() + "";
                break;
            case NUMERIC:
                value = cell.getNumericCellValue() + "";
                break;
            case FORMULA:
                value = cell.getCellFormula();
                break;
            case ERROR:
                value = "非法字符";
                break;
            default:
                value = "";
                break;
            }
            return value;
        }

    对于xlsx格式的excel,与上者的区别在于将HSSF***换成XSSF***,样例如下:

        public List<String[]> readExcel(File file){
            List<String[]> list = new ArrayList<>();
            
            try {
                XSSFWorkbook xw = new XSSFWorkbook(new FileInputStream(file));
                
                for (int i = 0; i < xw.getNumberOfSheets(); i++) {
                    XSSFSheet xs = xw.getSheetAt(i);
                    if(xs.getLastRowNum() == 0)
                        continue;
                    
                    for (int j = 0; j <= xs.getLastRowNum(); j++) {
                        XSSFRow row = xs.getRow(j);
                        String[] array = new String[row.getLastCellNum()];
                        
                        for (int k = 0; k <= row.getLastCellNum(); k++) {
                            XSSFCell cell = row.getCell(k);
                            if(cell == null)
                                continue;
                            array[k] = this.getValue(cell);
                        }
                        list.add(array);
                    }
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            return list;
        }
        
        private String getValue(XSSFCell cell){
            String value;
            CellType type = cell.getCellTypeEnum();
            
            switch (type) {
            case STRING:
                value = cell.getStringCellValue();
                break;
            case BLANK:
                value = "";
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue() + "";
                break;
            case NUMERIC:
                value = cell.getNumericCellValue() + "";
                break;
            case FORMULA:
                value = cell.getCellFormula();
                break;
            case ERROR:
                value = "非法字符";
                break;
            default:
                value = "";
                break;
            }
            return value;
        }
  • 相关阅读:
    SVN Windows环境搭建,简洁演示
    SVN-linux配置
    链接文本在a标签内标签里也可以用driver.find_element_by_link_text
    selenium python自动化简明演示
    关键字中mysql数据库查询条件带中文无结果解决办法
    python 最短路径
    python 难度分割
    c语言实现一个高铁乘客管理系统
    Linux(Ubuntu)系统安装图文教程
    字符串排序
  • 原文地址:https://www.cnblogs.com/yxth/p/8482860.html
Copyright © 2011-2022 走看看