ext和springmvc结合的导入功能

总结一下ext和springmvc结合的导入功能

前台html:
var improtformSch = new Ext.form.FormPanel({
labelWidth : 120,
height : 110,
region : 'center',
layout : 'form',
width : 600,
labelAlign : 'right',
url : 'verifySchController.do',
fileUpload : true,
frame : true, // 设置圆角蓝色背景
items : [{
layout : 'column',
border : false,
items : [{
columnWidth : .8,
layout : 'form',
labelAlign : 'right',
border : false,
items : [{
fieldLabel : '上传文件',
xtype : 'textfield',
width : 300,
name : 'file',
id : 'upload',
inputType : 'file',
listeners : {
"render" : function() {
Ext.get('upload').on("change", function() {
var uploadFileName = Ext.getCmp('upload')
.getValue();
var a = uploadFileName.indexOf('.xlsx');
var b = uploadFileName.indexOf('.xls');
if (a == -1 && b == -1) {
Ext.Msg.alert('提示', "请选择excel文件.!");
return;
}
document.getElementById("verify").innerHTML = " ";
Ext.getCmp("importBtn").setDisabled(true);
})
}
}

}]
}, {
columnWidth : .2,
layout : 'form',
border : false,
items : [new Ext.Button({
text : '校验文件',
handler : function() {

var upload = document.getElementById('upload');
upload.select();

var realpath = document.selection.createRange().text;
var uploadFileName = Ext.getCmp('upload').getValue();
if (realpath == "") {
Ext.Msg.alert('信息', '请选择excel文件');
return;
}
improtformSch.getForm().submit({// 提交表单
url : 'verifySchController.do',
method : 'POST',
waitMsg : '数据校验中, 请稍等...',
success : function(form, action) {
var result = action.result;
if (result.verifyFlag == "success") {
Ext.getCmp("importBtn").setDisabled(false);
document.getElementById("verify").innerHTML = "<div>校验开始...</div><div>"
+ result.verifyResults.success
+ "</div><div>校验结束...</div>";
} else if (result.verifyFlag == "warn") {
var divs = "";
var msgs = result.verifyResults.warn;
Ext.getCmp("importBtn").setDisabled(false);
for (var i = 0; i < msgs.length; i++) {
divs += "<div>告警信息:" + msgs[i]
+ "</div>";
}
document.getElementById("verify").innerHTML = "<div>校验开始...</div>"
+ divs + "<div>校验结束...</div>";
} else if (result.verifyFlag == "error") {
var divs = "";
var msgs = result.verifyResults.error;
for (var i = 0; i < msgs.length; i++) {
divs += "<div>失败信息:" + msgs[i]
+ "</div>";
}
document.getElementById("verify").innerHTML = "<div>校验开始...</div>"
+ divs + "<div>校验结束...</div>";
} else if (result.verifyFlag == "failure") {
document.getElementById("verify").innerHTML = "<div>校验开始...</div><div>"
+ result.verifyResults
+ "</div><div>校验结束...</div>";
}
},
failure : function() {
improtformSch.getForm().reset();
},
params : {}
});
}
})]
}]
}],
buttonAlign : 'center'
});


后台代码:
public class VerifySchController extends JsonProviderController {
@Autowired
private UploadFileParseService uploadFileParseService;

private Logger log=Logger.getLogger(VerifySchController.class);

private final static String UPLOAD_SUCCESS = "{success:true,mess:'文件上传成功!'}";
private final static String UPLOAD_FAILURE = "{success:false,mess:'文件上传失败!'}";
private final static String FILE_NO = "{success:false,mess:'文件不存在!'}";
private final static String FILE_YES = "{success:true,mess:'文件存在!'}";
private final static String CONTENT_TYPE = "text/html;charset=UTF-8";
private final static String APPLICATION = "application/octet-stream";

private String uploadDir = "/tmp";

public String getUploadDir() {
return uploadDir;
}

public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}

@RequestMapping(method = { RequestMethod.POST })
protected Object handleJsonRequest(HttpServletRequest request,
HttpServletResponse response) {
response.setContentType(CONTENT_TYPE);
if (!ServletFileUpload.isMultipartContent(request)) {
return UPLOAD_FAILURE;
}
String realPath = request.getRealPath(this.getUploadDir());
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("file");
byte[] bytes = file.getBytes();
File uploadFile=new File(realPath+"/"+file.getOriginalFilename());
FileCopyUtils.copy(bytes, uploadFile);

Map<String,Object> maps=uploadFileParseService.uploadFileParse(uploadFile);
maps.put("success", true);
if(maps.get("verifyFlag").equals("error")){
uploadFile.delete();
log.info("[调度操作]:调度流校验失败,操作时间:"+BasicUtil.abDate(new Date()));
}else{
log.info("[调度操作]:调度流校验成功,操作时间:"+BasicUtil.abDate(new Date()));
}
return maps;
} catch (Exception e) {
log.error("[调度操作]:调度流校验失败,操作时间:"+BasicUtil.abDate(new Date()));
e.printStackTrace();
}
return "{success:false,verifyFlag:'failure',verifyResults:' 校验失败...'}";
}
}

你可能感兴趣的:(springMVC,Excel,ext)