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

 

posted @ 2018-02-28 11:55  一响贪欢  阅读(801)  评论(0编辑  收藏  举报