Excel的批量导入是很常见的功能,这里采用 Jxl实现,数据量或样式要求较高可以采用 poi
框架环境:Spring + SpringMvc(注解实现)
1.首先导入依赖jar包
1
2
3
4
5
|
< dependency > < groupId >net.sourceforge.jexcelapi</ groupId > < artifactId >jxl</ artifactId > < version >2.6.10</ version > </ dependency > |
2.前端页面–jsp(enctype必须为"multipart/form-data" )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> < div style = "width: 600px;" > < form class = "form" method = "post" data-submit = "ajax" enctype = "multipart/form-data" action = "/hpersonnel/import.do" > //< input type = "hidden" name = "id" value = "<%= request.getParameter(" id") %>" /> < div class = "form-group" > < label class = "form-label w100" > </ label > < label class = "form-label " >1、下载模板。 < a class = "btn btn-default submit-form" href = "/template/personnel.xls" rel = "external nofollow" >下载</ a > </ label > </ div > < div class = "form-group" > < label class = "form-label w100" > </ label > < label class = "form-label " >2、按照模板表头填写内容,请勿修改文件格式</ label > </ div > < div class = "form-group" > < label class = "form-label w100" > </ label > < label class = "form-label " >3、< input type = "file" name = "file" /></ label > </ div > < div class = "btns dialog-button" > < button class = "dialog-btn-yes" type = "submit" >提交</ button > < button class = "dialog-btn-no" type = "button" onclick = "app.closeDialog(this)" >取消</ button > </ div > </ form > </ div > |
3.视图模板
4.Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
@ResponseBody @RequestMapping (value = "import" ) public Object import (MultipartFile file) { if (file.isEmpty()) { return ResultObject.failure( "文件为空" ); } ResultObject result = new ResultObject(); //记录集合 List<Map> mapList = new ArrayList<Map>(); //校验结果 boolean reqFlag = true ; //回复消息 String reqMsg = "" ; //报错消息数 Integer error = 0 ; //解析文件 try { //转换成输入流 InputStream is = file.getInputStream(); //得到excel Workbook workbook = Workbook.getWorkbook(is); //得到sheet Sheet sheet = workbook.getSheet( 0 ); //得到列数 int colsNum = sheet.getColumns(); //得到行数 int rowsNum = sheet.getRows(); if (rowsNum == 1 ) return ResultObject.failure( "没有数据" ); //单元格 Cell cell; //数据校验 for ( int i = 1 ; i < rowsNum; i++) { //第一行是标题,所以i从1开始 Map<Integer, Object> map = new HashMap<Integer, Object>(); for ( int j = 0 ; j < colsNum; j++) { cell = sheet.getCell(j, i); //第一个参数是列.第二个参数是行 if (j < 4 && "" .equals(cell.getContents())) { //----这里判断必填项(前4列) reqFlag = false ; reqMsg += "第" + (i + 1 ) + "行错误,错误信息:" + "必填项缺漏" ; reqMsg += "<br>" ; error++; break ; } String cellString = cell.getContents(); cellString = cellString.trim(); switch (j) { case 1 : { //进行校验处理,例如手机号 if (!StringUtil.isMobileNo(cellString)) { reqFlag = false ; reqMsg += "第" + (i + 1 ) + "行错误,错误信息:" + "联系电话有误" ; reqMsg += "<br>" ; error++; } else { map.put(j, cellString); } break ; } case 2 : { ### break ; } case 3 : { ### break ; } //无需校验,归入default default : { map.put(j, cell.getContents()); } } } if (reqFlag) { //校验通过 mapList.add(map); } } } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } //入库 try { if (mapList.size() > 0 && reqFlag) { //此处try,catch应优化为事务处理maplist实现全记录成功或失败 result.setSuccess( "提交成功" ); } else { if (error > 10 ) { //设置要显示的错误数 int index = StringUtil.getIndex(reqMsg, 10 , "<br>" ); reqMsg = reqMsg.substring( 0 , index + 4 ); reqMsg += "未显示错误数:" + (error - 10 ) + "条" ; reqMsg += "<br>" ; } result.setFailure(reqMsg); } } catch (Exception e) { result.setFailure( "入库错误,请联系管理员!" ); e.printStackTrace(); } return result; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_42840183/article/details/83275272