前段代码(easyUI):
1、导入文件插件:
function importXlsFile() {
html = '<form id="fileForm" method="post" enctype="multipart/form-data" >'
+ '          '
+ '<table><tr>'
+ '<td class="head" noWrap>选择文件上传:</td><td>'
+ '<input type="file" name="uploadFile" id="uploadFile" style="width:200px"/>'
+ '</tb></tr>' + '<table></form>';
openImportDialog('excel导入', html);//导入数据
}
2、导入数据
function openImportDialog(t, html) {
if ($('#excelDialog').length < 1) {
$(
'<div/>',
{
id : 'excelDialog',
title : '批量导入',
html : "<div id='import'>"
+ "</br>"
+ "          "
+ html + "</div>" + "</div>"
}).appendTo('body');
}
$('#excelDialog')
.dialog(
{
modal : true,
resizable : false,
dragable : false,
closable : false,
open : function() {
$('#excelDialog').css('padding', '0.8em');
$('#excelDialog .ui-accordion-content').css(
'padding', '0.4em').height(
$('#excelDialog').height() - 100);
},
buttons : [
{
text : '确定',
handler : function() {
var file = document.getElementById('uploadFile').value;
if (/^.+\.(xls|XSL)$/.test(file)) {
$.messager.progress();
var action = '';
action = appUrl+"后台处理数据的链接(action/controller)";
var form = document.getElementById('fileForm');
form.action = action;
form.target = "hideFrame";
form.submit();
} else {
$.messager.alert("提示", "请导入xls文件");
}
}
},
{
text : '取消',
handler : function() {
window.location.href = window.location.href;
$('#excelDialog').dialog('close');
}
} ],
width : document.documentElement.clientWidth * 0.28,
height : document.documentElement.clientHeight * 0.50
});
}
3、java后台处理数据的链接(action/controller)
a、action或者controller处理
public String importXls(){
if (StringUtils.isEmpty(uploadFileFileName)) {
setFailMessage("导入失败");
return RESULT_MESSAGE;
}
String fileName = this.uploadFileFileName.substring(this.uploadFileFileName.length() - 3,
this.uploadFileFileName.length());
if("xls".equalsIgnoreCase(fileName)){
Map<String, Object> map=kunnrBusinessService.importXls(uploadFile);
String msg=(String)map.get("resultMessage");
if (StringUtils.isNotEmpty(msg)) {
setFailMessage(msg);//返回错误信息
}else{
dealerList=(List<DealerDetail>)map.get("dealerList");
this.getSession().setAttribute("dealerist", dealerAdujstList);//将导入的数据放在session中
setSuccessMessage("导入成功");
}
}else {
setFailMessage("导入的文件格式错误");
}
}
b、service处理
public Map<String, Object> importDealerAdjustmentXls(File uploadFile) {
//读取数据表全局设置
Map<String, Object> map=new HashMap<String,Object>();
Workbook workbook = null;
Sheet sheet = null;
StringBuilder errorMsgContent = new StringBuilder();
List<DealerAdujstDetail> dealerAdujstDetailList=new ArrayList<DealerAdujstDetail>();
//读表逻辑处理
try {
//读取工作表
workbook=new HSSFWorkbook(new FileInputStream(uploadFile));
//获取表头
String[] header=new String[8];
header[0]="组织";
header[1]="代码";
header[2]="名称";
header[3]="年";
header[4]="月";
header[5]="项";
header[6]="项名称";
header[7]="箱";
//开始读取数据表格
sheet=workbook.getSheetAt(0);
int row=sheet.getLastRowNum();
if(row==0){
errorMsgContent.append("导入的Excel为空!</br>");
}else if(sheet.getRow(0).getLastCellNum() != header.length){
errorMsgContent.append("导入的Excel列数与下载文件列数不一致!</br>");
}else{
for (int i = 0; i < header.length; i++) {
if (!header[i].equals(getValue(sheet.getRow(0).getCell(i)))) {
errorMsgContent.append("第").append(i + 1).append("列")
.append(getValue(sheet.getRow(0).getCell(i)))
.append("与模板中").append(header[i]).append("不一致");
}
}
if (errorMsgContent.length() > 0) {
map.put("resultMessage", errorMsgContent.toString());
return map;
}
//将相应的值赋值给页面
for (int i = 1; i<row+1; i++) {
//字段设置
String org="";
String Id="";
String kunnr="";
String Year="";
String Month="";
String matter="";
String matterName="";
String adjust="";
//赋值
orgName=getValue(sheet.getRow(i).getCell(header.length-8));
kunnrId=getValue(sheet.getRow(i).getCell(header.length-7));
kunnrName=getValue(sheet.getRow(i).getCell(header.length-6));
applyYear=getValue(sheet.getRow(i).getCell(header.length-5));
applyMonth=getValue(sheet.getRow(i).getCell(header.length-4));
matter=getValue(sheet.getRow(i).getCell(header.length-3));
matterName=getValue(sheet.getRow(i).getCell(header.length-2));
adjustTarget=getValue(sheet.getRow(i).getCell(header.length-1));
//写入的值的判断
if(StringUtils.isNotEmpty(orgName)&&StringUtils.isNotEmpty(kunnrId)&&StringUtils.isNotEmpty(kunnrName)){
if(count==0){
errorMsgContent.append("第" + (i) + "行:名称+代码(注意:形如010056)+组织必须不一致</br>");
}
}else{
errorMsgContent.append("第" + (i) + "行:经销商名称+代码(注意:形如01010056)+组织必须不能为空</br>");
}
if (StringUtils.isNotEmpty(applyYear)&&StringUtils.isNotEmpty(applyMonth)) {
//2、只能导入提报当月及以后月份
if (!applyYear.equals(year) || !applyMonth.equals(month)) {
errorMsgContent.append("第" + (i + 1) + "行:申请年月不符合要求(注意:月份形如06或者10)!</br>");
}
}else{
errorMsgContent.append("第" + (i + 1) + "行:"+header[3]+"不能为空!(注意:月份形如06或者10)</br>");
}
if (StringUtils.isNotEmpty(matter)&&StringUtils.isNotEmpty(matterName)) {
//品项判断
if (count==0) {
errorMsgContent.append("第" + (i + 1) + "行:品项不符合要求!</br>");
}
}else{
errorMsgContent.append("第" + (i + 1) + "行:"+header[5]+"不能为空!</br>");
}
//3、项、数量必须大于等于目标
if (StringUtils.isNotEmpty(adjustTarget)) {
//首先判断是否是数字
for (int m_digit = 0; m_digit < adjustTarget.length(); m_digit++) {
char c = adjustTarget.charAt(m_digit);
if ((c < '0' || c > '9')) {
if(c!='.' && c!='-'){
errorMsgContent.append("第" + (i + 1) + "行:"
+ header[7] + "为非数字的值!</br>");
break;
}
}
}
if (detailList.size()!=0) {
for (DealerAdujstDetail detail1 : detailList) {
if(Long.valueOf(adjustTarget)<=Long.valueOf(detail1.getNowDealerTarget())){
errorMsgContent.append("第" + (i + 1) + "行:调整目标量不符合要求!</br>");
}
}
}
}else{
errorMsgContent.append("第" + (i + 1) + "行:"+header[5]+"不能为空!</br>");
}
//字段赋值
}
}
}
map.put("resultMessage", errorMsgContent.toString());
map.put("dealerList", dealerList);
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}