1. 程式人生 > >讀取excel工具類自適應兩種excel版本

讀取excel工具類自適應兩種excel版本

excel版本有兩種,如果不做出一個自適應的工具類對於程式碼的健壯性有重大影響。

程式碼如下:

@Component
public class ImportExcelUtil {
    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel
    /**
     * 根據url讀取Excel資料
     * @return List<Map<String,String>> 表格每一行資料在一個List中,每一單元格存在List中的map中
     * 每一行中的單元格依次為cell0,cell1,cell2....
     * @Throws IOException
     */
    public List<Map<String,String>> readExcel(File file) throws IOException {
        if(file != null){

            FileInputStream fis = new FileInputStream(file);
            String fileName = file.getName();
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            List<Map<String,String>> list = new ArrayList<>();

            if(excel2003L.equals(fileType)){
                HSSFWorkbook workbook = new HSSFWorkbook(fis);  //2003-
                HSSFSheet sheet = workbook.getSheetAt(0);
                //從第二行開始迴圈拿資料
                for(int i=1;i<=sheet.getLastRowNum();i++){
                    HSSFRow xr = sheet.getRow(i);
                    Map<String,String> map = new HashMap<>();
                    //從此行第一列開始迴圈拿資料
                    for(int j=0;j<xr.getLastCellNum();j++){
                        HSSFCell codeCell = xr.getCell(j);
                        codeCell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        //設定map,格式為cellX(X為此單元格的列數),值
                        if(codeCell.getStringCellValue().equals("-")){
                            map.put("cell"+j,null);
                        }
                        else{
                            map.put("cell"+j,codeCell.getStringCellValue());
                        }
                    }
                    list.add(map);
                }
                return list;
            }
            else if(excel2007U.equals(fileType)){
                XSSFWorkbook workbook = new XSSFWorkbook(fis);  //2007+
                XSSFSheet sheet = workbook.getSheetAt(0);

                //從第二行開始迴圈拿資料
                for(int i=1;i<=sheet.getLastRowNum();i++){
                    XSSFRow xr = sheet.getRow(i);
                    Map<String,String> map = new HashMap<>();
                    //從此行第一列開始迴圈拿資料
                    for(int j=0;j<xr.getLastCellNum();j++){
                        XSSFCell codeCell = xr.getCell(j);
                        codeCell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        //設定map,格式為cellX(X為此單元格的列數),值
                        if(codeCell.getStringCellValue().equals("-")){
                            map.put("cell"+j,null);
                        }
                        else{
                            map.put("cell"+j,codeCell.getStringCellValue());
                        }
                    }
                    list.add(map);
                }
                return list;
            }else{
                return null;
            }

    }else {
            return null;
        }

  }
}