---
title: "extjs文件上传导入及 本地模版下载"
author: "hanjava"
date: "2017-08-28T16:20:45Z"
tags: []
categories: ["Web前端"]
slug: "extjs"
draft: false
lastmod: "2017-08-28T16:20:45Z"
id: 2391327
url: "https://hanjava.iteye.com/blog/2391327"
---

logisticLockSettingFormImport = Ext.extend(Ext.Window, {
    formPanel: null,
    constructor: function(a) {
        Ext.applyIf(this, a);
        this.initUIComponents(a.colData);
        logisticLockSettingFormImport.superclass.constructor.call(this, {
            id: "logisticLockSettingFormImportWin",
            layout: "fit",
            iconCls: "menu-icWorkPlan",
            items: this.formPanel,
            modal: true,
            autoHeight: true,
            width: 525,
            minWidth: 525,
            maximizable: false,
            resizable: false,
            title: "导入",
            buttonAlign: "center",
            buttons: this.buttons,
            keys: {
                key: Ext.EventObject.ENTER,
                fn: this.save.createCallback(this.formPanel, this),
                scope: this
            }
        });
    },
    initUIComponents: function(tb) {
        this.formPanel = new Ext.FormPanel({
            layout: "form",
            autoHeight: true,
            fileUpload:true,
            frame: false,
            labelAlign: "right",
            labelWidth: 40,
            defaults: {
                width: 500
            },
            border: false,
            url: __ctxPath + "/logic/logisIlockExcelImportLogicIlock.do",
            id: "logisticLockSettingFormImport",
            bodyStyle: "padding: 0px 5px 0px 5px",
            items: *)\??.*$/, '$1');//获得文件的后缀名
                            var temp1 = temp.toLocaleLowerCase();
                            //alert(temp1);
                            if (allowfiletype.indexOf(temp1) == -1) {
                                Ext.Msg.alert("错误","不允许选择该类型文件，请重新选择！");
                                fb.setValue("");
                                FileRname.setValue("");
                            }
                        }
                    }
                },{
            	    xtype : 'label',
            	    html : '[<font color=red>智能锁导入模版下载</font>](./doc/logis_ilock.xlsx)'
                }]
            }]
        });

        this.buttons = ;
    },
    reset: function(a) {
        a.getForm().reset();
    },
    cancel: function(a) {
        a.close();
    },
    save: function(a, b) {
        if (a.getForm().isValid()) {
//        	var filenPath = Ext.getCmp("uploadExcel").getValue();
//        	filenPath=filenPath.replace(/\\/g, "\\\\");//将\转换成\\
//        	alert("filenPath==="+filenPath);
//        	Ext.Ajax.request({ 
//        		url: __ctxPath + "/logic/logisIlockExcelImportLogicIlock.do?uploadExcel="+filenPath, //导入接口
//				success: function(d) {
//        			alert("=="+d.responseText);
//					var g = Ext.util.JSON.decode(d.responseText);
//					console.log(g);
//					if(g.success==true){
//						Ext.ux.Toast.msg("操作信息", "成功导入信息！"+g.data);
//	                    var d = Ext.getCmp("logicIlockpanel");
//	                    if (d != null) {
//	                        d.getStore().reload();
//	                    }
//	                    b.close();
//					}else{
//						Ext.MessageBox.show({
//	                        title: "操作信息",
//	                        msg: "信息保存出错，请联系管理员！"+g.data,
//	                        buttons: Ext.MessageBox.OK,
//	                        icon: Ext.MessageBox.ERROR
//	                    });
//	                    b.close();
//					}
//				}
//			});
            a.getForm().submit({
                method: "POST",
                waitMsg: "正在导入数据...",
                success: function(c, e) {
                    Ext.ux.Toast.msg("操作信息", "成功保存信息！");
                    var d = Ext.getCmp("logicIlockpanel");
                    if (d != null) {
                        d.getStore().reload();
                    }
                    b.close();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });
                    b.close();
                }
            });
        }
    }
});

	/**
	 * 信息导入后台代码
	 * @return
	 * @throws Exception
	 *http://localhost:8080/HEMS/logic/logisIlockExcelImportLogicIlock.do
	 */
	 public String logisIlockExcelImport() throws Exception{
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		req.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		
		//获取要保存文件夹的物理路径(绝对路径)
        String realPath=ServletActionContext.getServletContext().getRealPath("/attachFiles/");
        File tempFile = new File(realPath);
        if(!tempFile.exists()){
        	tempFile.mkdirs();
        }
        
		List<String> list = null;
		try {
			 //保存文件
			FileUtils.copyFile(uploadExcel, new File(tempFile,uploadExcelFileName));
//			String tempPath = F:\\logis_ilock.xlsx";
	        //获得导入的文件路径
	        String savePath=ServletActionContext.getServletContext().getRealPath("/attachFiles/"+this.uploadExcelFileName);
			File importFile = new File(savePath);
            list = ExcelHelper.exportListFromExcel(importFile, 0);//读取excel按自己需求解析，这里不提供
            importFile.delete();
            
			String fnumber=null;//编号名称
			String fcode=null;//锁编码
			String fphone=null;//手机号
			String strLockSupplier=null;//锁厂家
			String fdescription=null;//备注
			
			String strs=null;//每行内容信息
			LogisIlock lock = null;
			
			StringBuffer buff=new StringBuffer("{\"success\":true").append(",\"data\":\"");
			for (String str : list) {
				strs=str.split(",");//测试数据,863014530683340,13526541534,星安,测试测试测试哈哈
				fnumber=strs;
				fcode=strs;
				fphone=strs;
				strLockSupplier=strs;
				fdescription=strs;
				
				lock = logicIlockService.getLogisIlock(fcode);
				if(null!=lock){
					if(buff.length()<25){
						buff.append(fcode+"锁编码已经存在");
					}else{
						buff.append(","+fcode+"锁编码已经存在");
					}
				}else{
					lock=new LogisIlock();
					lock.setFnumber(fnumber);
					lock.setFcode(fcode);
					lock.setFphone(fphone);
					lock.setLockSupplier(Integer.valueOf(DataDictionaryUtils.lockSupplier(strLockSupplier)));
					lock.setFdescription(fdescription);
					lock.setFstatus(0);
					lock.setFisDel(0);
					lock.setFcreatorId(ContextUtil.getCurrentUser().getUserId());
					lock.setFdepId(ContextUtil.getCurrentUser().getDepartment()==null?null:ContextUtil.getCurrentUser().getDepartment().getDepId());
					lock.setForgId(ContextUtil.getCurrentUser().getOrgId());
					lock.setFcreateTime(new Date());
					this.logicIlockService.save(lock);
					if(buff.length()<25){
						buff.append(fcode+"锁编码导入成功");
					}else{
						buff.append(","+fcode+"锁编码导入成功");
					}
				}
			}
			buff.append("\"}");
			this.jsonString=buff.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}