poi 文件导入 xls(Excel) 表格

版权声明:帅气Dee海绵宝宝 https://blog.csdn.net/xyjcfucdi128/article/details/83537971

先引入依赖

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

接着是 controller 层

        /**
	     * 
	     * @param request
	     * @return 导入ecxel
	     * @throws Exception 
	     */
	    @RequestMapping("/importFT")
	    @ResponseBody
	    public ResponseVO importExcelFT(MultipartFile excelFile, HttpServletRequest request) throws Exception{
	    	  try {
	    		  if(!excelFile.isEmpty()){
		                String fileName = excelFile.getOriginalFilename();
		                
		                System.out.println(fileName);
		                tBusFunctionService.importExcel(fileName, excelFile);
		                return ResponseVO.ok("导入数据成功");
		            }else{
		            	 return ResponseVO.ok("请选择文件");
		            }
		        } catch (Exception e) {
		            e.printStackTrace();
		            return ResponseVO.error("导入数据失败");
		        }

	    }

接着是 service 层代码(我这里是将导入的引入四张表)

    BusSystem querySystemOne(BusSystem entity);//用来poi 导入查询 t_bus_system表
	
	void saveSystem(BusSystem entity);// 用来 poi插入 t_bus_system表
	
	BusDomain queryDomain(BusDomain entity);//用来 poi 导入 查询 t_bus_domain
	
	void saveDomain(BusDomain entity);//用来  poi 导入的  插入 t_bus_domain表
	
	void importExcel(String fileName, MultipartFile excelFile) throws Exception;//poi导入

再接着是serviceImpl 层

1.这个是一个在serviceImpl 层的 方法 用来 获取对应列的 数据

private String getCellValue(Cell cell) {
		String value = "";
		  if (cell != null) {
		   // 以下是判断数据的类型
		   switch (cell.getCellType()) {
		   case HSSFCell.CELL_TYPE_NUMERIC: // 数字
		    value = cell.getNumericCellValue() + "";
		    if (HSSFDateUtil.isCellDateFormatted(cell)) {
		     Date date = cell.getDateCellValue();
		     if (date != null) {
		      value = new SimpleDateFormat("yyyy-MM-dd").format(date);
		     } else {
		      value = "";
		     }
		    } else {
		     value = new DecimalFormat("0").format(cell.getNumericCellValue());
		    }
		    break;
		   case HSSFCell.CELL_TYPE_STRING: // 字符串
		    value = cell.getStringCellValue();
		    break;
		   case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
		    value = cell.getBooleanCellValue() + "";
		    break;
		   case HSSFCell.CELL_TYPE_FORMULA: // 公式
		    value = cell.getCellFormula() + "";
		    break;
		   case HSSFCell.CELL_TYPE_BLANK: // 空值
		    value = "";
		    break;
		   case HSSFCell.CELL_TYPE_ERROR: // 故障
		    value = "非法字符";
		    break;
		   default:
		    value = "未知类型";
		    break;
		   }
		  }
		  return value.trim();
	}

 2.poi 导入的 serviceImpl 的实现 

@Override
	public void importExcel(String fileName, MultipartFile excelFile) throws Exception {
		// TODO Auto-generated method stub
		 //  1、用HSSFWorkbook打开或者创建“Excel文件对象”
		  //
		  //  2、用HSSFWorkbook对象返回或者创建Sheet对象
		  //
		  //  3、用Sheet对象返回行对象,用行对象得到Cell对象
		  //
		  //  4、对Cell对象读写。
		  //获得文件名 
          // getCellValue(row.getCell(i)) 这里是获取i列所对应的数据 
		
		Workbook workbook = null ;
		  if(fileName.endsWith(XLS)){ 
		   //2003 
		   workbook = new HSSFWorkbook(excelFile.getInputStream()); 
		  }else if(fileName.endsWith(XLSX)){ 
		   //2007 
		   workbook = new XSSFWorkbook(excelFile.getInputStream()); 
		  }else{
		   throw new Exception("文件不是Excel文件");
		  }
		  Sheet sheet = workbook.getSheet("Sheet1");
		  int rows = sheet.getLastRowNum();// 指的行数,一共有多少行+
		  if(rows==0){
		   throw new Exception("请填写数据");
		  }
		  
		  
		  for (int i = 1; i <= rows+1; i++) {
			   // 读取左上端单元格
			   Row row = sheet.getRow(i);
			   // 行不为空
			   if (row != null) {//逻辑存储代码 调用相应的 dao层进行存储
				   
				   //先判断 function 中有没有 name等于导入的名字 
				   BusFunction busFunction = new BusFunction();
				   busFunction.setName(getCellValue(row.getCell(3)));
				   
				   if(!busFunction.getName().equals("")) {
					   
						   
					   
					   
					   BusFunction querybusFunction = queryOne(busFunction);
					   if(querybusFunction==null) {//没有 直接导入 function 和 table
						   String busFunctionId = Uuid.getUUid();
						   
						   //插入到 t_bus_function 表中
						   busFunction.setId(busFunctionId);
						   busFunction.setDesc(getCellValue(row.getCell(4)));
						   BusSystem busSystem = new BusSystem();//查询 systemId
						   busSystem.setName(getCellValue(row.getCell(1)));
						   BusSystem queryBusSystem = querySystemOne(busSystem);
						   busFunction.setSystemId(queryBusSystem.getSystemId());
						   save(busFunction);//插入到 t_bus_function 表中
						   
						   //把数据添加 到 busTable中
						   BusTable busTable= new BusTable();
						   busTable.setCode(getCellValue(row.getCell(5)));
						   if(busTable.getCode()!=null||!busTable.getCode().equals("")) {
							   
							   busTable.setId(Uuid.getUUid());
							   busTable.setName(getCellValue(row.getCell(6)));
							   busTable.setDesc(getCellValue(row.getCell(7)));
							   busTable.setFunId(busFunctionId);//把数据存储到 t_bus_table中
							   tbusTableDao.save(busTable);
							   
							   
						   }
						   
					   }else {//如果有 在查询 table中的英文名称有没有和导入一样的  有就跳过 没有直接导入
						   BusTable bt= new BusTable();
						   bt.setCode(getCellValue(row.getCell(5)));
						   if(!bt.getCode().equals("")||bt.getCode()!=null) {
							   BusTable queryBusTable = tbusTableDao.queryOne(bt);//先查询 t_bus_table 中是否有相同名称的 
							   if(queryBusTable==null) {//没有相同名称的 插入  有相同的 直接跳过
								   BusTable bustable= new BusTable();
								   bustable.setId(Uuid.getUUid());
								   bustable.setName(getCellValue(row.getCell(6)));
								   bustable.setCode(getCellValue(row.getCell(5)));
								   bustable.setDesc(getCellValue(row.getCell(7)));
								   bustable.setFunId(querybusFunction.getId());
								   tbusTableDao.save(bustable);
							   }
						   }
						   
					   }
				   }
				   
				
			   }
			  }
		
		
		
	}

猜你喜欢

转载自blog.csdn.net/xyjcfucdi128/article/details/83537971