Skip to content

Commit 2704763

Browse files
committed
upload excel for insert user
1 parent 084c123 commit 2704763

File tree

10 files changed

+2525
-40
lines changed

10 files changed

+2525
-40
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* bootstrap3
99
* bootstrap-table1.9
1010
* druid
11-
* weui
11+
* [weui](https://github.com/weui/weui)
12+
* [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload)
1213

1314
# 微信扫码使用gitee工具
1415
![gitee工具](https://s1.ax1x.com/2018/08/10/P60MMF.jpg)

pom.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
</developers>
2020

2121
<properties>
22+
<java.version>1.8</java.version>
2223
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2324
<jfinal.version>4.8</jfinal.version>
2425
<freemarker.version>2.3.30</freemarker.version>
2526
<poi.version>4.1.2</poi.version>
27+
<cos.version>2020.4</cos.version>
2628
</properties>
2729

2830
<dependencies>
@@ -31,6 +33,12 @@
3133
<artifactId>jfinal</artifactId>
3234
<version>${jfinal.version}</version>
3335
</dependency>
36+
<!-- https://mvnrepository.com/artifact/com.jfinal/cos -->
37+
<dependency>
38+
<groupId>com.jfinal</groupId>
39+
<artifactId>cos</artifactId>
40+
<version>${cos.version}</version>
41+
</dependency>
3442
<dependency>
3543
<groupId>org.freemarker</groupId>
3644
<artifactId>freemarker</artifactId>
@@ -150,8 +158,8 @@
150158
<artifactId>maven-compiler-plugin</artifactId>
151159
<version>3.1</version>
152160
<configuration>
153-
<source>1.7</source> <!-- 源代码使用的开发版本 -->
154-
<target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->
161+
<source>8</source> <!-- 源代码使用的开发版本 -->
162+
<target>8</target> <!-- 需要生成的目标class文件的编译版本 -->
155163
<!--手动指定maven编译插件编译源码时使用的编码-->
156164
<!--<encoding>utf-8</encoding>-->
157165
</configuration>

src/main/java/cn/netbuffer/jfinal_bootstrap_table/controller/IndexController.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.jfinal.kit.PropKit;
1616
import com.jfinal.plugin.ehcache.CacheInterceptor;
1717
import lombok.extern.slf4j.Slf4j;
18+
import org.apache.commons.collections4.CollectionUtils;
1819
import org.apache.commons.lang3.RandomUtils;
1920
import org.apache.commons.lang3.builder.ToStringBuilder;
2021
import java.io.File;
@@ -86,6 +87,9 @@ public void newdata() {
8687
public void export() {
8788
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
8889
List<User> users = User.dao.find("select * from " + ConfigConstant.USERTABLE + " limit 50");
90+
if (CollectionUtils.isEmpty(users)) {
91+
renderText("暂无数据");
92+
}
8993
String projectPath = getRequest().getServletContext().getRealPath("export");
9094
int userCount = users.size();
9195
List<Map<String, Object>> mps = new ArrayList<Map<String, Object>>(users.size());
@@ -95,13 +99,13 @@ public void export() {
9599
}
96100
log.info("mps:{}", mps.toString());
97101
List<String> titles = new ArrayList<String>(mps.get(0).size() - 1);
98-
titles.add("adddate");
99-
titles.add("age");
100-
titles.add("deliveryaddress");
101102
titles.add("id");
102103
titles.add("name");
103-
titles.add("phone");
104+
titles.add("age");
104105
titles.add("sex");
106+
titles.add("phone");
107+
titles.add("adddate");
108+
titles.add("deliveryaddress");
105109
String file = projectPath + format.format(new Date()) + "." + ConfigConstant.EXCELSTR;
106110
POIExcelUtil.write(titles, mps, file);
107111
renderFile(new File(file));

src/main/java/cn/netbuffer/jfinal_bootstrap_table/controller/UserController.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import cn.netbuffer.jfinal_bootstrap_table.model.User;
55
import cn.netbuffer.jfinal_bootstrap_table.service.IUserService;
66
import cn.netbuffer.jfinal_bootstrap_table.service.impl.UserServiceImpl;
7+
import cn.netbuffer.jfinal_bootstrap_table.util.POIExcelUtil;
78
import com.jfinal.aop.Aop;
89
import com.jfinal.aop.Before;
910
import com.jfinal.core.Controller;
10-
import com.jfinal.plugin.ehcache.CacheInterceptor;
11+
import com.jfinal.upload.UploadFile;
1112
import lombok.extern.slf4j.Slf4j;
1213
import java.util.HashMap;
14+
import java.util.List;
1315
import java.util.Map;
1416

1517
/**
@@ -30,7 +32,7 @@ public void index() {
3032
/**
3133
* 开启缓存
3234
*/
33-
@Before({CacheInterceptor.class})
35+
// @Before({CacheInterceptor.class})
3436
public void userlist() {
3537
int limit = getParaToInt("limit");
3638
int offset = getParaToInt("offset");
@@ -62,4 +64,22 @@ public void update() {
6264
renderText(String.valueOf(userservice.update(user)));
6365
}
6466

67+
public void upload() {
68+
UploadFile file = getFile("excel");
69+
log.debug("接收文件上传:{}", file.getFileName());
70+
Map data = new HashMap();
71+
data.put("name", file.getFileName());
72+
List<List<String>> excelData = POIExcelUtil.read(file.getFile().getAbsolutePath());
73+
excelData.forEach(e -> {
74+
User user = new User();
75+
user.setName(e.get(0));
76+
user.setAge((int) Double.parseDouble(e.get(1)));
77+
user.setSex(e.get(2));
78+
user.setAdddate((int) System.currentTimeMillis() / 1000);
79+
log.debug("save user[{}]:{}", user, user.save());
80+
});
81+
data.put("count", excelData.size());
82+
renderJson(data);
83+
}
84+
6585
}

src/main/java/cn/netbuffer/jfinal_bootstrap_table/interceptor/AuthInterceptor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jfinal.aop.Interceptor;
55
import com.jfinal.aop.Invocation;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.apache.commons.lang3.StringUtils;
78
import org.apache.commons.lang3.builder.ToStringBuilder;
89

910
/**
@@ -16,13 +17,12 @@ public class AuthInterceptor implements Interceptor {
1617

1718
@Override
1819
public void intercept(Invocation inv) {
19-
log.info("Before [{}] method invoking",inv.getMethodName());
20+
log.info("Before [{}] method invoking", inv.getMethodName());
2021
Object login = inv.getController().getSession().getAttribute(ConfigConstant.ISLOGIN);
21-
log.info("invoking:" + inv.getControllerKey() + "--" + inv.getMethodName() + "--" + inv.getViewPath() + "--" + ToStringBuilder.reflectionToString(inv.getArgs()));
22-
//未登录跳转
23-
if (inv.getMethodName().equals("login") || inv.getMethodName().equals("captcha") || inv.getControllerKey().contains("register")
24-
|| (null != login && Boolean.parseBoolean(login.toString()))) {
25-
// //传递本次调用,调用剩下的拦截器与目标方法
22+
log.info("invoking controller[{}]method[{}]view[{}]args[{}]\nlogin status:{}", inv.getControllerKey(), inv.getMethodName(), inv.getViewPath(),
23+
ToStringBuilder.reflectionToString(inv.getArgs()), login);
24+
if (StringUtils.equalsAny(inv.getMethodName(), "login", "captcha") || inv.getControllerKey().contains("register") || (null != login && Boolean.parseBoolean(login.toString()))) {
25+
//传递本次调用,调用剩下的拦截器与目标方法
2626
inv.invoke();
2727
} else {
2828
inv.getController().renderJavascript("<script type='text/javascript'>alert('您还未登录,不能执行此操作!');</script>");

src/main/java/cn/netbuffer/jfinal_bootstrap_table/util/POIExcelUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public static List<List<String>> read(String path) {
4343
int cells = row.getPhysicalNumberOfCells();
4444
List<String> arr = new ArrayList<>(cells);
4545
for (int i = 0; i < cells; i++) {
46-
arr.add(row.getCell(i).getStringCellValue());
46+
CellType cellType = row.getCell(i).getCellType();
47+
if (cellType == CellType.NUMERIC) {
48+
arr.add(String.valueOf(row.getCell(i).getNumericCellValue()));
49+
} else if (cellType == CellType.STRING) {
50+
arr.add(row.getCell(i).getStringCellValue());
51+
}
4752
}
4853
data.add(arr);
4954
}

src/main/webapp/manage.html

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<link href="static/bootstrap/bootstrap.min.css" rel="stylesheet">
88
<link href="static/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
99
<link href="static/bootstrap-datetimepicker.min.css" rel="stylesheet">
10+
<style>
11+
.hide-file {
12+
opacity: 0;
13+
position: absolute;
14+
top: 55px;
15+
background-color: red;
16+
z-index: 10000;
17+
height: 33px;
18+
width: 100px;
19+
left: 195px;
20+
}
21+
</style>
1022
</head>
1123
<body>
1224
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
@@ -76,9 +88,9 @@
7688
onclick="exporyAll();">
7789
<span class="glyphicon glyphicon-arrow-down"></span> 导出历史数据到excel
7890
</button>
79-
<button type="button" class="btn btn-default"
80-
onclick="import_from_xls();">
81-
<span class="glyphicon glyphicon-arrow-up"></span> 导入用户
91+
<button type="button" class="btn btn-default">
92+
<span class="glyphicon glyphicon-arrow-up"></span>
93+
<input class="hide-file" type="file" data-url="user/upload" name="excel" id="fileupload"/> 导入用户
8294
</button>
8395
<table id="dtb" data-toggle="table" data-url="user/userlist"
8496
class="table table-hover" data-show-columns="true" data-search="true"
@@ -165,8 +177,24 @@ <h4 class="modal-title" id="myModalLabel">今日新增的用户数据</h4>
165177
<script src="static/moment.min.js"></script>
166178
<script src="static/moment.zh-cn.js"></script>
167179
<script src="static/bootstrap-datetimepicker.min.js"></script>
180+
<script src="static/jquery-file-upload/jquery.ui.widget.js"></script>
181+
<script src="static/jquery-file-upload/jquery.fileupload.js"></script>
168182
<script type="text/javascript">
169183
$(function () {
184+
$("#fileupload").fileupload({
185+
add: function (e, data) {
186+
console.log("data:%o", data.files[0]);
187+
alert("上传文件:" + data.files[0].name);
188+
data.submit();
189+
},
190+
done: function (e, data) {
191+
console.log("上传完成:%o", data);
192+
if (data.result) {
193+
alert("成功插入" + data.result.count + "条数据");
194+
$("#dtb").bootstrapTable('refresh');
195+
}
196+
}
197+
});
170198
$.getJSON('newdata', function (data) {
171199
$("#xinzeng").text(data.newcount);
172200
$("#xinzeng").tooltip({"title": "今日新增数据,点击查看", "placement": "bottom"});
@@ -206,8 +234,10 @@ <h4 class="modal-title" id="myModalLabel">今日新增的用户数据</h4>
206234
//alert('You click remove icon, row: ' + JSON.stringify(row));
207235
var delid = $("[data-index=" + index + "]").find('td').eq(0).children().first().val();
208236
if (confirm("确定删除该条记录吗?" + delid)) {
209-
$.getJSON('user/delete', {"id": delid}, function (data) {
210-
$("#dtb").bootstrapTable('refresh');
237+
$.post('user/delete', {"id": delid}, function (data) {
238+
if (data.status == "success") {
239+
$("#dtb").bootstrapTable('refresh');
240+
}
211241
});
212242
}
213243
console.log(value, row, index);

src/main/webapp/register-success.html

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
6-
<title>注册页</title>
7-
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/weui/0.3.0/style/weui.min.css" />
8-
<link rel="stylesheet" type="text/css" href="css/register.css" />
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
6+
<title>注册成功</title>
7+
<link rel="stylesheet" type="text/css" href="https://cdn.bootcdn.net/ajax/libs/weui/2.3.0/style/weui.min.css"/>
98
</head>
109
<body ontouchstart>
11-
<div class="container">
12-
<div class="weui_msg">
13-
<div class="weui_icon_area"><i class="weui_icon_success weui_icon_msg"></i></div>
14-
<div class="weui_text_area">
15-
<h2 class="weui_msg_title">注册成功</h2>
16-
<p class="weui_msg_desc">您的注册成功啦</p>
10+
<div class="container" id="container">
11+
<div class="page msg_success js_show">
12+
<div class="weui-msg">
13+
<div class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></div>
14+
<div class="weui-msg__text-area">
15+
<h2 class="weui-msg__title">注册成功</h2>
16+
<p class="weui-msg__desc">内容详情,可根据实际需要安排,如果换行则不超过规定长度,居中展现<a href="javascript:">文字链接</a></p>
17+
</div>
18+
<div class="weui-msg__opr-area">
19+
<p class="weui-btn-area">
20+
<a href="javascript:history.back();" class="weui-btn weui-btn_primary">继续注册</a>
21+
</p>
22+
</div>
23+
<div class="weui-msg__tips-area">
24+
<p class="weui-msg__tips">提示详情,可根据实际需要安排,如果换行则不超过规定长度,居中展现<a href="javascript:">文字链接</a></p>
25+
</div>
26+
<div class="weui-msg__extra-area">
27+
<div class="weui-footer">
28+
<p class="weui-footer__links">
29+
<a href="javascript:" class="weui-footer__link">底部链接文本</a>
30+
</p>
31+
<p class="weui-footer__text">Copyright © 2008-2016 weui.io</p>
32+
</div>
33+
</div>
1734
</div>
18-
<div class="weui_opr_area">
19-
<p class="weui_btn_area">
20-
<a class="weui_btn weui_btn_primary" href="javascript:;">确定</a>
21-
<a class="weui_btn weui_btn_default" href="javascript:;">取消</a>
22-
</p>
23-
</div>
24-
<div class="weui_extra_area">
25-
<a href="">查看详情</a>
35+
36+
<div class="page__ft j_bottom">
37+
<a href="javascript:home()"><img src="images/icon_footer_link.png"></a>
2638
</div>
2739
</div>
2840
</div>

0 commit comments

Comments
 (0)