Fork me on GitHub

实现 xls 数据的导入

实现 xls 数据的导入

html

1
2
3
4
5
6
7
8
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
onclick="batchAddClient()">导入信息</a>
<!-- 导入框 -->
<div id="importCheckWin" class="easyui-dialog" data-options="onBeforeOpen:getDialogCheckUrl,
closed:true" style="width:489px; height:268px;">
<iframe frameborder="0" id = "impIframeCheck" width="100%"
height="100%" scrolling="no" src=""></iframe>
</div>

js 方法

1
2
3
4
function getDialogCheckUrl(){
var url = getHost(HostAddress.uomp)+'/uomp/cdaBaseDataRemarks/importCheckWin';
$('#impIframeCheck').attr('src',url);
}

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
@RequestMapping(value = "/cdaBaseDataRemarks")
public class CdaBaseDataRemarksController extends BaseController {
/**
* 打开批量检查导入窗口
* @param locale
* @param model
* @return
*/
@RequestMapping(value = "/importCheckWin", method = RequestMethod.GET)
public String importCheckWin(Locale locale, Model model) {
model.addAttribute("uihost", SysConstant.config.getProperty("uihost"));
return "importBatchRemarksCheck";
}
}

jsp 页面

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>批量检查客户窗口</title>
<script type="text/javascript"
src="${uihost}/sunriseui/sunriseui.js"></script>
<script type="text/javascript">
load('ajaxfileupload')
</script>
<script type="text/javascript">
var map;var index=0; var mapLenght=0;/*map长度 */
var successNum=0;/*成功条数*/
var failNum='';/*错误行号*/
//存在跨域问题 不能够获取到返回值 -- 批量检查提交
function coreSelect() {
$('#tipgrid').empty();
$.ajaxFileUpload({
url : getHost(HostAddress.uomp) + '/uomp/cdaBaseDataRemarks/importBatchClientCheckData',
secureuri : false, // 是否需要安全协议,一般设置为false
id : "filesform",
fileElementId : 'files', // 文件上传域的ID
dataType : 'json', // 返回值类型 一般设置为json
data : {
},
async : false,
success : function(data){// 服务器成功响应处理函数
if(data.returnCode==ReturnCode.success){
$('#importRotaff').form('reset');
$('#tipgrid').datagrid({
data:data,
title:'导入结果',
fitColumns:false,
border:false,
columns:[[
{field:'relustMsg',title:'备注信息导入结果',width:500}
]]
});
$('#tipgrid').append("<span>"+data.returnMessage+'</span>');
}else{
$.messager.alert('警告', data.returnMessage,'warning');
}
}
});
}
function fileChange(target){
var fileSize = 0;
if(!target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scriting.FileSystemObject");
var file = fileSystem.GetFile(filePath);
fileSize = file.Size;
}else{
fileSize = target.files[0].size;
}
var size = fileSize/1024;
if(size>3000){
$.messager.alert('警告','导入数据超过3M,请分批导入!','warning');
$('#subBtn').attr('disable');
return;
}else{
$('#subBtn').linkbutton('enable');
}
}
</script>
</head>
<body class="easyui-layout" data-options="fit:true">
<div data-options="region:'center'" style="background: #eee;">
<form id="importRotaff" action="" enctype="multipart/form-data">
<table style="width: 100%">
<tr>
<td colspan="2">
<input type="file" name="files" onchange="fileChange(this)" id="files" style="width: 200px">
<a id="subBtn" href="#"
class="easyui-linkbutton" iconCls="icon-search" onclick="coreSelect()">导入信息</a>
</td>
</tr>
<tr>
<td align="center" id="thistd"></td>
</tr>
</table>
</form>
<div id="tipgrid"></div>
</div>
</body>
</html>

数据导入 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
@Controller
@RequestMapping(value = "/cdaBaseDataRemarks")
public class CdaBaseDataRemarksController extends BaseController {
/**
* 读取批量检查Excel导入
* @param locale
* @param model
* @return
*/
@RequestMapping(value = "/importBatchClientCheckData", method = RequestMethod.POST)
@ResponseBody
public ResultBean<LogImportBean> importBatchClientCheckData(Locale locale,
@RequestParam MultipartFile[] files,@ModelAttribute("CdaBaseDataRemarks")
CdaBaseDataRemarks bean) throws Exception{
ResultBean<LogImportBean> rb = new ResultBean<LogImportBean>();
//返回导入结果明细信息列表
List<LogImportBean> logs = new ArrayList<LogImportBean>();
rb.setRows(logs);
if(files[0].getSize()==0){
rb.setReturnCode(SysConstant.SYS_RETURN_FAILED_CODE);
rb.setReturnMessage("未选择文件,请选择您要导入的execl文件");
return rb;
}
InputStream is = files[0].getInputStream();
Workbook book = Workbook.getWorkbook(is);
Sheet sheet = book.getSheet(0);
int rownum = sheet.getRows();
// 解析到表格中的数量,判断是否超过导入个数限制
if (rownum > 1) {
rb.setReturnCode(SysConstant.SYS_RETURN_SUCCESS_CODE);
rb.setReturnMessage(SysConstant.SYS_RETURN_SUCCESS_MESSAGE);
for (int i = 1; i < rownum; i++) {
//第一个参数是列,第二个参数是行
String custId = sheet.getCell(0, i).getContents();
String certNo = sheet.getCell(1, i).getContents();
String certType = sheet.getCell(2, i).getContents();
String remarks = sheet.getCell(3, i).getContents();
// 判断字符串是否在大于20
if(remarks.length()>20){
logs.add(new LogImportBean("备注内容不能大于20个字"));
}
// 新增自己的业务代码
}// for 循环结束
}else{
rb.setReturnCode(SysConstant.SYS_RETURN_FAILED_CODE);
rb.setReturnMessage("表格不能为空!");
}
return rb;
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!