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