java--java操作excel并将数据塞入数据库--SSM框架

个人书写记录
技术 渣渣—还望勿喷!
需求:
根据excel模板放入对应的患者(客户)相关信息,然后前端上传excel,后台接收并读取,将表头中文改为数据库对应的字段名,并放入对应的数据,若是表头是空或者不对,没按照规定即可提醒前端,
jar----
xmlbeans-2.6.0.jar(下载)
poi-ooxml-schemas-3.17-beta1.jar(下载)
poi-ooxml-3.17-beta1.jar(下载)
poi-3.17-beta1.jar(下载)
commons-collections4-4.1.jar(下载)
思路:
将文件保存到本地盘符,然后再读取操作。

	@RequestMapping(value = "/xxxx", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> UploadFile(@RequestBody(required = false)@RequestParam("excelPatientFile")  MultipartFile excelPatientFile) throws IOException {

			Map<String, Object> ret = new HashMap<String, Object>();
		 	String fileName = "";
	        if (excelPatientFile != null && !excelPatientFile.isEmpty()){
	            fileName = excelPatientFile.getOriginalFilename();
	            System.err.println(fileName);
	        }else {
	        	ret.put("msg", "上传的文件为空,请重新选择。");
	        }
		// 判断文件是否为空
		if (!excelPatientFile.isEmpty()) {
			try {
				// 文件保存路径
				String filePath = "D:\\"
						+ excelPatientFile.getOriginalFilename();
				// 转存文件
				excelPatientFile.transferTo(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Workbook wb =null;
	        Sheet sheet = null;
	        Row row = null;
	        List<Map<String,String>> list = null;
	        String cellData = null;
	        String filePath = "D:\\"+fileName;
	        String [] columns = new String[2];
	        String [] bd = {"姓名","住院号"}; 
	        wb = readExcel(filePath);
	        if(wb != null){
	            //用来存放表中数据
	            list = new ArrayList<Map<String,String>>();
	            //获取第一个sheet
	            sheet = wb.getSheetAt(0);
	           
	            //获取最大行数
	            int rownum = sheet.getPhysicalNumberOfRows();
	            //获取第一行
	            row = sheet.getRow(0);
	            //获取最大列数
	            int colnum = row.getPhysicalNumberOfCells();
	            System.err.println("rownum"+colnum);
	           //遍历字段名,将对应的字段名改为数据库字段名存入数组
	            for (int i = 0; i<colnum; i++) {
	            	if((boolean) getCellFormatValue(row.getCell(i)).equals("")){
	            		continue;
	            	}
	            	System.err.println((String) getCellFormatValue(row.getCell(i)));
	            	//查看字符bd数组中包含的有没有这个字段名
	            	if(Arrays.asList(bd).contains((String) getCellFormatValue(row.getCell(i)))){ 						
	            		if((boolean) getCellFormatValue(row.getCell(i)).equals("姓名")){
	            			columns[i]="brxm";
	            			continue;
	            		}
	            		if((boolean) getCellFormatValue(row.getCell(i)).equals("住院号")){
	            			columns[i]="zyh";
	            			continue;
	            		}
	            	
	            	}else{
	            		ret.put("msg", (String) getCellFormatValue(row.getCell(i))+":字段有问题!");
	            		ret.put("success", false);
	            		return ret;
	            	}	            	
	            }
	          	//将数据遍历放到集合中
	            for (int i = 1; i<rownum; i++) {
	                Map<String,String> map = new LinkedHashMap<String,String>();
	                row = sheet.getRow(i);
	                if(row !=null){
	                    for (int j=0;j<colnum;j++){
	                    	String today = DateUtil.getToday();
	                    	
	                        cellData = (String) getCellFormatValue(row.getCell(j));
	        
	                        map.put(columns[j], cellData);
	                    
	                    }
	                }else{
	                    break;
	                }
	                
	                list.add(map);
	            }
	        }
	        //遍历解析出来的list
	        System.err.println("list"+list);
	        for (Map<String,String> map : list) {
	            for (Entry<String,String> entry : map.entrySet()) {
	                System.out.print(entry.getKey()+":"+entry.getValue()+",");
	            }
	            System.out.println();
	        }  
	        //传入list数据给mybatis操作
	        int num = systemService.addHzxx(list);
	        
	        if(num>0){
	        	ret.put("msg", "导入成功");
	        	ret.put("success", true);
	        }else{
	        	ret.put("msg", "导入成功");
	        	ret.put("success", false);
	        }
		return ret;
	}

```sql
<insert id="addHzxx" parameterType="java.util.List">
	<foreach collection="paramList" item="item" index="index">
		INSERT INTO patient (zyh,txh)
		VALUES (#{item.zyh},#{item.txh});
	</foreach>   
</insert> 

你可能感兴趣的:(java--java操作excel并将数据塞入数据库--SSM框架)