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> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值