springboot+vue利用POI技术实现对Excel的导入功能并插入到数据库

第一次接触Excel导出功能,写的有点费劲,把代码贴出来希望能帮到大家

后端:springboot+springcloud+mybatis
前端:vue+iview
数据库:oracle

先上vue


        

uploadLoading: false,
file: null,
    //导入
  handleFormatError(file){
    this.$Notice.warning({
      title: '文件格式不正确',
      desc: '文件 ' + file.name + ' 格式不正确,请上传.xls,.xlsx文件。'
    })
  },
  handleSuccess(res,file){
      this.$Message.success("数据导入成功!")
      //提示完渲染页面
      this.getTreeData(1)
  },
  handleError(error,file){
    this.$Message.error("数据导入失败!")
  },

接下来我们写后台
导入jar包(不知道是否都能用到,引就vans了,版本可能对代码有影响)


    
      org.apache.poi
      poi-ooxml
      3.9
    
    
      org.apache.poi
      poi
      3.9
    
    
      org.dom4j
      dom4j
      2.1.1
    
    
      org.apache.poi
      poi-ooxml-schemas
      3.9
    
    
      org.apache.poi
      poi-scratchpad
      3.9
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
    
      commons-io
      commons-io
      2.4
    
    
      org.apache.xmlbeans
      xmlbeans
      2.3.0
    
    
      org.apache.poi
      poi-examples
      3.9
    
    
      org.apache.poi
      poi-excelant
      3.9
    

controller

/**
	 * 导入Excel
	 * @param file
	 * @throws Exception
	 */
	@ApiOperation(value="Excel导入", notes="上传Excel表格插入数据库")
	@RequestMapping(value = "/upload",method = RequestMethod.POST)
	public void upload(@RequestParam("file") MultipartFile file) throws Exception {
		stationService.batchImport(file);
	}

service

/**
	 * 导入Excel
	 * @param file
	 * @throws Exception
	 */
	void batchImport(MultipartFile file) throws Exception;

serviceimpl

/**
	 * 导入Excel
	 * @param file
	 * @throws Exception
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public void batchImport(MultipartFile file)throws Exception  {
		List ss = new ArrayList<>();
		InputStream is = file.getInputStream();
		Workbook workbook = WorkbookFactory.create(is);
		//确定版本
		boolean isExcel2003 = file.getOriginalFilename().endsWith("xls")?true:false;
		List> returnMap;
		if(isExcel2003) {
			//有多少个sheet
			int sheets = workbook.getNumberOfSheets();
			for (int i = 0; i < sheets; i++) {
				HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(i);
				//获取多少行
				int rows = sheet.getPhysicalNumberOfRows();
				Station s = null;
				//遍历每一行,注意:第 0 行为标题
				for (int j = 1; j < rows; j++) {
					s = new Station();
					//获得第 j 行
					HSSFRow row = sheet.getRow(j);
					//转换格式 row.getCell(0).setCellType(Cell.CELL_TYPE_STRING)
					s.setStationName(row.getCell(0).getStringCellValue());
					s.setContacts(row.getCell(1).getStringCellValue());
					s.setAddress(row.getCell(2).getStringCellValue());
					s.setIntoUseTime(row.getCell(3).getStringCellValue());
					ss.add(s);
					if(StringUtils.isBlank(s.getStationId())){
						s.setStationId(UUIDGenerate.getUUID());
					}
					Map  returnMap1=new HashMap<>();
					returnMap1.put("station",s);

					stationDao.addStationExcel(s);

				}

			}
		}else{
			//有多少个sheet
			int sheets = workbook.getNumberOfSheets();
			for (int i = 0; i < sheets; i++) {
				XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(i);
				//获取多少行
				int rows = sheet.getPhysicalNumberOfRows();
				Station s = null;
				//遍历每一行,注意:第 0 行为标题
				for (int j = 1; j < rows; j++) {
					s = new Station();
					//获得第 j 行
					XSSFRow row = sheet.getRow(j);
					//设置参数与Excel列对应
					s.setStationName(row.getCell(0).getStringCellValue());
					s.setContacts(row.getCell(1).getStringCellValue());
					s.setAddress(row.getCell(2).getStringCellValue());
					s.setIntoUseTime(row.getCell(3).getStringCellValue());
					ss.add(s);
					//设置id为随机数
					if(StringUtils.isBlank(s.getStationId())){
						s.setStationId(UUIDGenerate.getUUID());
					}
					stationDao.addStationExcel(s);
				}
			}
		}
	}

dao

/**
	 * Excel导入
	 * @param s
	 */
	void addStationExcel(Station s);

**.xml


	
		insert into 表名
		
			
				station_id,
			
			
				station_name,
			
			
				contacts,
			
			
				into_use_time,
			
			
				address,
					
		
		
			
				#{stationId,jdbcType=VARCHAR},
			
			
				#{stationName,jdbcType=VARCHAR},
			
			
				#{contacts,jdbcType=VARCHAR},
			
			
				to_date(#{intoUseTime,jdbcType=DATE},'yyyy-MM-dd'),
			
			
				#{address,jdbcType=VARCHAR},
			
		
	

你可能感兴趣的:(springboot+vue利用POI技术实现对Excel的导入功能并插入到数据库)