2025年10月8日 星期三

V20108E – [匯入人員清單] - SelectFile(Ok_process) ; - 挑選檔案 - XLS2XLS - 傳回匯入結果XLS,並顯示執行訊息 -New

 目的: V20108E – 工作程序資源設定 – [匯入人員清單] - SelectFile(Ok_process) ;  
           - 挑選檔案  - XLS2XLS - 傳回匯入結果XLS, 並顯示執行結果訊息
           - 傳送檔案至前端

處理說明: 1>*.js 挑選檔案子畫面 - SelectFile(Ok_process);  -Ok_process 挑選後執行func
                        1>>SelectFile [挑選檔案子畫面]確定後,會將檔案傳送至後端
                        2>>讀取檔案名稱(*.js)  
                               var Tmp_FileObj = Ext.getCmp('FName');
                               var Tmp_FName = Ext.getDom(Tmp_FileObj.inputId).value;
                 2>*.cs 檔案傳至後端  - Request.Files[0]   
                        Stream file_Strm = Request.Files[0].InputStream;
                        wk.LoadFromStream(file_Strm);
                        file_Name = Path.GetFileName(Request.Files[0].FileName);
                3>*.cs  wk 存至檔案  & Stream 
                    wk = new Workbook();
                    MemoryStream mstream = new MemoryStream();
            string documentPath = HttpContext.Current.Server.MapPath("~") + "document\\";  // 實實路徑  
                    string FileName = "V20108_匯入人員清單_匯入結果說明.xlsx";
                    string FileName1 = documentPath + FileName;
                    wk.SaveToStream(mstream, FileFormat.Version2007);
                    wk.SaveToFile(FileName1, FileFormat.Version2007);
              4>  傳送結果檔案至前端
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName, System.Text.Encoding.UTF8) + "\"");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.BinaryWrite(mstream.ToArray()); 
              5> 顯示執行結果 - 透過 Cookie
                   me.up("form").submit({
                         url: Tmp_url_Str,
                        method: 'POST',
                         headers: { 'Content-type': 'multipart/form-data' },
                         params: np,        
                 });
                 //顯示結果訊息..
                var mask = new Ext.LoadMask(Ext.getBody(), {
                      msg: '處理中,請稍待...'
                 });
                 mask.show();//使用 mask 需手動呼叫show() 方法下
                //若要傳送檔案到前端, 必需用 Cookie 判斷後端是否已完成
               //每1秒檢核一次,是否已完成, 若已完成,則不再檢核
                var timer = setInterval(function () {
                var r = r_cookies('Rtn_Msg');
                 if (!checkisnull(r)) {
                     mysuccessalert(r);
                     clearInterval(timer);
                      mask.hide();
                      timer = null;
                     //將子畫面關閉- SelectFile
                     Ext.getCmp("SelectForm_cancelbtn").fireHandler();          
        }
    }, 1000);  //1000ms = 1sec  

        //*.cs - 加入 Cookie 訊息
            Tmp_RtnMsg = "人員清單匯入成功!!";                        
            MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_RtnMsg));
            HttpContext.Current.Response.Cookies.Add(MyCookie);            
            HttpContext.Current.Response.End();






1>*.js
var pub_MAIN_SUB1_Str = "";
function CALL_V20108E_OK() {
        var Tmp_MAIN_SUB1;
        Tmp_MAIN_SUB1 = Ext.getCmp('sub_MAIN_SUB1').getValue();
        pub_MAIN_SUB1_Str = Tmp_MAIN_SUB1["sub_MAIN_SUB1"];  //EMP/DEV
        console.log("pub_MAIN_SUB1_Str:", pub_MAIN_SUB1_Str);
        var Tmp_FName = "";
        SelectFile(Ok_process);
        console.log("Tmp_FName:", Tmp_FName);        
};

//[選擇檔案]子畫面. [確認匯入]鈕 , 處理
function Ok_process(me, e, eOpts) {
    //取得 [檔案上傳].檔名
    var Tmp_FileObj = Ext.getCmp('FName');
    var Tmp_FName = Ext.getDom(Tmp_FileObj.inputId).value;
    console.log("FName Obj:", Tmp_FileObj);
    var Tmp_Str = "";
    var Tmp_cur_recs = Ext.getCmp("grid_M").getSelectionModel().getSelection();
    var Tmp_cur_rec = Tmp_cur_recs[0];
    var Tmp_cur_MITM = Tmp_cur_rec.data["MITM"];
    var Tmp_cur_WITM = Tmp_cur_rec.data["WITM"];    
    console.log("Tmp_cur_MITM:", Tmp_cur_MITM);

    var np = {};
    np["MAIN_SUB"] = pub_MAIN_SUB1_Str;
    np["cur_MITM"] = Tmp_cur_MITM;
    np["cur_WITM"] = Tmp_cur_WITM;
    np["FName"] = Tmp_FName;
    
    console.log("np.FName: ", np.FName);
    console.log("np.MAIN_SUB: ", np.MAIN_SUB);
    if (pub_MAIN_SUB1_Str="EMP")
        Tmp_url_Str = '../api/V20108EAPI/uploadFileToDB_EMP';
    else
        Tmp_url_Str = '../api/V20108EAPI/uploadFileToDB_DEV';
    me.up("form").submit({
        //standardSubmit: false,    //default:false
        url: Tmp_url_Str,
        method: 'POST',
        headers: { 'Content-type': 'multipart/form-data' },
        params: np,        
    });

    //顯示結果訊息..
    var mask = new Ext.LoadMask(Ext.getBody(), {
        msg: '處理中,請稍待...'
    });

    mask.show();//使用 mask 需手動呼叫show() 方法下
    //若要傳送檔案到前端, 必需用 Cookie 判斷後端是否已完成
    //每1秒檢核一次,是否已完成, 若已完成,則不再檢核
    var timer = setInterval(function () {

        var r = r_cookies('Rtn_Msg');
        //console.log("r_cookies=", r);
        if (!checkisnull(r)) {
            mysuccessalert(r);
            clearInterval(timer);
            mask.hide();
            timer = null;
            //將視窗關閉
            Ext.getCmp("SelectForm_cancelbtn").fireHandler();
            //this.up("window").close();
            //this.up("window").destroy();
        }
    }, 1000);  //1000ms = 1sec
};  // end of Ok_process() {  //批次新增


2>*.cs
 public void uploadFileToDB_EMP()
        {

            HttpContext c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            string Tmp_MAIN_SUB = nvc["MAIN_SUB"];
            string Tmp_MITM = nvc["cur_MITM"];
            string Tmp_WITM = nvc["cur_WITM"];            
            string FName = nvc["FName"];
            Workbook wk = null;
            MemoryStream mstream = new MemoryStream();

            HttpRequest Request = HttpContext.Current.Request;
            var response = this.Request.CreateResponse();
            string file_Name = FName;
            string FILEDT = DateTime.Now.ToString("yyyy/MM/dd");
            string Tmp_RtnMsg = "";
            string Tmp_RtnMsg1 = "";
            string Tmp_Sql = "", Tmp_Str = "";
            bool Tmp_isOk;
            int Tmp_cnt = 0;

          string documentPath = HttpContext.Current.Server.MapPath("~") + "document\\";  // 實實路徑    
            string FileName = "V20108_匯入人員清單_匯入結果說明.xlsx";
            string FileName1 = documentPath + FileName;

            List<string> SQLStringList_A = new List<string>();
            HttpCookie MyCookie;  //將訊息傳至前端

            string fileType = Request.Files[0].ContentType;
            Stream file_Strm = Request.Files[0].InputStream;
            file_Name = Path.GetFileName(Request.Files[0].FileName);
            int fileSize = Request.Files[0].ContentLength;
                byte[] fileRcrd = new byte[fileSize];
                //更新資料庫欄位值                
                int Tmp_curpos = 0;
                try
                {

                    wk = new Workbook();
                    wk.LoadFromStream(file_Strm);
                    Worksheet sheet1 = wk.Worksheets[0];//獲取第一個工作表
                    //新增[匯入結果說明]欄位 - 於最後一欄位
                    sheet1.Range[myfunc.GetExcelPos(5, 0)].Value = "匯入結果說明";
                    Tmp_WC = sheet1.Range[myfunc.GetExcelPos(0, 0)].Value;
                     //若[欄位標題 ]有錯誤時,傳回錯誤訊息
                     MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_RtnMsg));
                     HttpContext.Current.Response.Cookies.Add(MyCookie);                    
                     HttpContext.Current.ApplicationInstance.CompleteRequest();
                         :
              }
                    wk.SaveToStream(mstream, FileFormat.Version2007);
                    wk.SaveToFile(FileName1, FileFormat.Version2007);
 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName, System.Text.Encoding.UTF8) + "\"");
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
                    if (Tmp_isOk == false)
                    {
                        //匯入檔案失敗
                        Tmp_Str = "人員清單匯入失敗<br>"
                                        + "(" + FName + ")<br>";
                        MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                        HttpContext.Current.Response.Cookies.Add(MyCookie);
                        HttpContext.Current.Response.End();
                        return;
                    }
                   

沒有留言:

張貼留言