导入Excel扩展名是.xls 和.xlsx的

1.首先是导入Excel2003以前(包括2003)的版本,扩展名是.xls 的


  1. /** 
  2.      * 操作Excel2003以前(包括2003)的版本,扩展名是.xls  
  3.      * @param templetFile 文件 
  4.      * @param startrow 开始行号 
  5.      * @param startcol 开始列号 
  6.      * @param sheetnum sheet 
  7.      * @return list 
  8.      */  
  9.     public static List<Map<String,String>> readExcelByXls(MultipartFile templetFile, int startrow, int startcol, int sheetnum) {  
  10.         List<Map<String,String>> varList = new ArrayList<Map<String,String>>();  
  11.   
  12.         try {  
  13.               
  14.             HSSFWorkbook wb = new HSSFWorkbook(templetFile.getInputStream());  
  15.             HSSFSheet sheet = wb.getSheetAt(sheetnum);                  //sheet 从0开始  
  16.             int rowNum = sheet.getLastRowNum() + 1;                     //取得最后一行的行号  
  17.   
  18.             for (int i = startrow; i < rowNum; i++) {                    //行循环开始  
  19.                   
  20.                 Map<String,String> varpd = new HashMap<String,String>();  
  21.                 HSSFRow row = sheet.getRow(i);                          //行  
  22.                 int cellNum = row.getLastCellNum();                     //每行的最后一个单元格位置  
  23.   
  24.                 for (int j = startcol; j < cellNum; j++) {               //列循环开始  
  25.           
  26.                     HSSFCell cell = row.getCell(Integer.parseInt(j + ""));  
  27.                     String cellValue = null;  
  28.                     if (null != cell) {  
  29.                         switch (cell.getCellType()) {                   // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库  
  30.                         case 0:  
  31.                             if(HSSFDateUtil.isCellDateFormatted(cell)){  
  32.                                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  33.                                 cellValue=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();  
  34.                             } else {  
  35.                                 cell.setCellType(1);  
  36.                                 cellValue = cell.getStringCellValue();  
  37.                             }  
  38.                             break;  
  39.                         case 1:  
  40.                             cellValue = cell.getStringCellValue();  
  41.                             break;  
  42.                         case 2:  
  43.                             //cell.setCellType(1);  
  44.                             //cellValue = cell.getStringCellValue();  
  45.                             //cellValue = cell.getNumericCellValue() + "";  
  46.                              cellValue = String.valueOf(cell.getDateCellValue());  
  47.                             break;  
  48.                         case 3:  
  49.                             cellValue = "";  
  50.                             break;  
  51.                         case 4:  
  52.                             cellValue = String.valueOf(cell.getBooleanCellValue());  
  53.                             break;  
  54.                         case 5:  
  55.                             cellValue = String.valueOf(cell.getErrorCellValue());  
  56.                             break;  
  57.                         }  
  58.                     } else {  
  59.                         cellValue = "";  
  60.                     }  
  61.                       
  62.                     varpd.put("var"+j, cellValue);  
  63.                       
  64.                 }  
  65.                 varList.add(varpd);  
  66.             }  
  67.   
  68.         } catch (Exception e) {  
  69.             System.out.println(e);  
  70.         }  
  71.           
  72.         return varList;  
  73.     }  


2.是操作Excel2007的版本,扩展名是.xlsx的


[javascript] view plain copy
  1. /** 
  2.      * 是操作Excel2007的版本,扩展名是.xlsx 
  3.      * @param templetFile 文件 
  4.      * @param startrow 开始行号 
  5.      * @param startcol 开始列号 
  6.      * @param sheetnum sheet 
  7.      * @return list 
  8.      */  
  9.     public static List<Map<String,String>> readExcelByXlsx(MultipartFile templetFile, int startrow, int startcol, int sheetnum) {  
  10.         List<Map<String,String>> varList = new ArrayList<Map<String,String>>();  
  11.   
  12.         try {  
  13.               
  14.             XSSFWorkbook wb = new XSSFWorkbook(templetFile.getInputStream());  
  15.             XSSFSheet sheet = wb.getSheetAt(sheetnum);                  //sheet 从0开始  
  16.             int rowNum = sheet.getLastRowNum() + 1;                     //取得最后一行的行号  
  17.   
  18.             for (int i = startrow; i < rowNum; i++) {                    //行循环开始  
  19.                   
  20.                 Map<String,String> varpd = new HashMap<String,String>();  
  21.                 XSSFRow row = sheet.getRow(i);                          //行  
  22.                 int cellNum = row.getLastCellNum();                     //每行的最后一个单元格位置  
  23.   
  24.                 for (int j = startcol; j < cellNum; j++) {               //列循环开始  
  25.                       
  26.                     XSSFCell cell = row.getCell(Integer.parseInt(j + ""));  
  27.                     String cellValue = null;  
  28.                     if (null != cell) {  
  29.                         switch (cell.getCellType()) {                   // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库  
  30.                         case 0:               
  31.                             if(HSSFDateUtil.isCellDateFormatted(cell)){  
  32.                                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  33.                                 cellValue=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();  
  34.                             } else {  
  35.                                 cell.setCellType(1);  
  36.                                 cellValue = cell.getStringCellValue();  
  37.                             }  
  38.                             break;  
  39.                         case 1:  
  40.                             cellValue = cell.getStringCellValue();  
  41.                             break;  
  42.                         case 2:  
  43.                             cellValue = cell.getStringCellValue();  
  44.                             //cellValue = cell.getNumericCellValue() + "";  
  45.                             // cellValue = String.valueOf(cell.getDateCellValue());  
  46.                             break;  
  47.                         case 3:  
  48.                             cellValue = "";  
  49.                             break;  
  50.                         case 4:  
  51.                             cellValue = String.valueOf(cell.getBooleanCellValue());  
  52.                             break;  
  53.                         case 5:  
  54.                             cellValue = String.valueOf(cell.getErrorCellValue());  
  55.                             break;  
  56.                         }  
  57.                     } else {  
  58.                         cellValue = "";  
  59.                     }  
  60.                       
  61.                     varpd.put("var"+j, cellValue);  
  62.                       
  63.                 }  
  64.                 varList.add(varpd);  
  65.             }  
  66.   
  67.         } catch (Exception e) {  
  68.             System.out.println(e);  
  69.         }  
  70.           
  71.         return varList;  
  72.     }  


3.重点来了,结合前两个代码使用,自动识别excel版本文件

 

    1. public static List<Map<String,String>> readExcel(MultipartFile templetFile, int startrow, int startcol, int sheetnum){  
    2.         List<Map<String,String>> varList = new ArrayList<Map<String,String>>();  
    3.         if(templetFile!=null&&templetFile.getSize()>0){  
    4.             String ofn=templetFile.getOriginalFilename();// 文件名  
    5.             String extName = ""; // 扩展名格式:  
    6.             if (ofn.lastIndexOf(".") >= 0){  
    7.                 extName = ofn.substring(ofn.lastIndexOf("."));  
    8.             }  
    9.             if(".xls".equals(extName.toLowerCase())){  
    10.                 varList=readExcelByXls(templetFile,startrow,startcol,sheetnum);  
    11.             }else if(".xlsx".equals(extName.toLowerCase())){  
    12.                 varList=readExcelByXlsx(templetFile,startrow,startcol,sheetnum);  
    13.             }  
    14.         }  
    15.         return varList;  
    16.     } 
posted @ 2017-12-07 01:24  海底一小鱼  阅读(452)  评论(0编辑  收藏  举报