1:upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="./layui/css/layui.css" media="all"> </head> <body> <div class="layui-container"> <div class="layui-row" align="center" style="margin-top: 30px;"> <button type="button" class="layui-btn" id="upload"> <i class="layui-icon"></i>选择文件</button> </div> <div class="layui-row" align="center" style="margin-top: 30px;"> <textarea id="result" cols="50" rows="10"></textarea> </div> </div> </body> <script src="./jquery/jquery.min.js"></script> <script src="./layui/layui.js"></script> <script> layui.use('upload', function(){ var upload = layui.upload; //执行上传 var uploadInst = upload.render({ elem: '#upload' //绑定元素 ,url: '/ssfwpt/ra/ramanage' //上传接口 ,method: 'POST' ,accept: 'file' ,size: 50 ,before: function(obj){ layer.load(); } ,done: function(res){//上传完毕回调 layer.closeAll('loading'); var result = ''; for(var i=0; i<res.length; i++){ result = result + res[i].nsrsbh+"="+res[i].container+" "; } $("#result").html(result); } ,error: function(){//请求异常回调 layer.closeAll('loading'); layer.msg('网络异常,请稍后重试!'); } }); }); </script> </html>
2:后台(Spring-boot)
/** * 实现文件上传 * */ @RequestMapping(value = "/ramanage", method = RequestMethod.POST) @ResponseBody public List<Map<String,String>> ramanage(@RequestParam("file") MultipartFile file){ List<Map<String,String>> result = new ArrayList<>(); try { InputStream input = file.getInputStream(); Workbook wb = new HSSFWorkbook(input); Sheet sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum()+1; Map<String,String> map; for(int i=1; i<rowNum; i++){ Row row = sheet.getRow(i); //容器名称 Cell containerCell = row.getCell(0); String container = containerCell.getStringCellValue(); //税号 Cell nsrsbhCell = row.getCell(1); String nsrsbh = nsrsbhCell.getStringCellValue(); map = new HashMap<>(); map.put("nsrsbh", nsrsbh); map.put("container", container); result.add(map); } } catch (IOException e) { e.printStackTrace(); } return result; }