2021-06-09

导出Excel到istMap中工具类

/**
 * 将Excel数据读取到ListMap
 * @param file
 */
  public static List<Map<String, Object>> readExcelDataToListMap(MultipartFile file) {
      String edition_2003 = "^.+\\.(?i)(xls)$";		// Excel_2003版本
      String edition_2007 = "^.+\\.(?i)(xlsx)$";	// Excel_2007版本
      String fileName = file.getOriginalFilename();	// 获取文件名
      if (StringUtils.isNotBlank(fileName)) {               
      	  try {       		
      	      // 根据Excel版本创建对象
      	      Workbook wb = null;
      	      if (fileName.matches(edition_2003)) {				 
      	          wb = new HSSFWorkbook(file.getInputStream());
      	      } else {
      		  if (fileName.matches(edition_2007)) {                		
      		      wb = new XSSFWorkbook(file.getInputStream());
      		  }
      	      }         		
      	      // 读取Excel里面的数据
      	      if (null != wb) {					
      	         // 得到第一个shell
      	          Sheet sheet = wb.getSheetAt(0);
      	    	  // 得到Excel的行数
      	    	  int totalRows = sheet.getPhysicalNumberOfRows();
      	    	  // 得到Excel的列数(前提是有行数)
      	    	  int totalCells = 0;
      	    	  if (totalRows > 1 && sheet.getRow(0) != null) {
      	    	      totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
      	    	  }
      	    	  List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
      	    	  // 循环Excel行数
      	    	  for (int r = 1; r < totalRows; r++) {
      	    	      Row row = sheet.getRow(r);
    	    	      if (row == null) {
    	    	          continue;
    	    	      }    		
    	    	      // 循环Excel的列
    	    	      Map<String, Object> map = new HashMap<String, Object>();
    	    	      for (int c = 0; c < totalCells; c++) {
    	    	          Cell cell = row.getCell(c);
    	    	          if (null != cell) {
    	    	              int cellType = cell.getCellType();   				
    	    	              if (cellType == 0) {    					
    	    	                  DataFormatter dataFormatter = new DataFormatter();
    	    	                  dataFormatter.addFormat("###########", null);
    	    	                  String str = dataFormatter.formatCellValue(cell);    					
    	    	                  if (StringUtils.isNotBlank(str)) {							
    	    	    	              map.put(c+"",str);
    		                  }
    	    	              }else{
    	    	                  String str = String.valueOf(cell);
    	                          if (StringUtils.isNotBlank(str)) {						
    	        	              map.put(c+"",str);
    	    	                  } 
    	    	              }
    	                  } 
    	              }
    	              // 添加到list
    	              if (map.size() > 0) {				
    	    	         listMap.add(map);
    	              }
      	          }
      	          return listMap;        			        			
               }
             } catch (Exception e) {
          	    e.printStackTrace();
             }
        }                        
        return null;
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值