2024年5月27日 星期一

V30204F – [批次匯入]鈕 – 若匯入資料有錯誤,則寫入*.xlsx, 傳至前端 - XLS2XLS

 目的:V30204F – [批次匯入] 若匯入資料有錯誤,則寫入*.xlsx, 傳至前端

處理說明:  1>出現[選擇檔案]子畫面, 若未選擇檔案,則顯示訊息
                        var Tmp_FileObj = Ext.getCmp('FName');
                        var Tmp_FName = Ext.getDom(Tmp_FileObj.inputId).value;
                        if (checkisnull(Tmp_FName)) {
 
                          Tmp_Str = "檔案名稱不可空白,敬請檢核!!"
                             myalert(Tmp_Str);
                            return;
                         }
                         par_OkProcess(this,e,eOpts);
                  2> 將檔案傳送至後端處理 
                       1>>*.js
                           var np = {}
                           np["FName"] = Ext.getDom(Tmp_FileObj.inputId).value;    
                           console.log("FName: ", np.FName);
                           //submit會將fileUploadForm裡面input name送到後端
                           me.up("form").submit({                       
                                url: '../api/V20304FAPI/uploadFileToDB',
                                method: 'POST',
                               headers: { 'Content-type': 'multipart/form-data' },
                               params: np,
                          });
                     2>*.cs  : 取得前端檔案
                         Stream file_Strm = Request.Files[cur_FName].InputStream;
                         file_Name = Path.GetFileName(Request.Files[cur_FName].FileName); 

            3>將 xls 檔案傳至前端
                wk.SaveToStream(mstream, FileFormat.Version2007);
                wk.SaveToFile(FileName1, FileFormat.Version2007);
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName1, 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());




1>*.js
//[單筆工時匯入]鈕 - - 匯入單筆工時資料,無子畫面
function Call_V20304F() {
      //挑選檔案後, 送給 Ok_prrocess 函式處理
     SelectFile(Ok_process);

}
function Ok_process(me, e, eOpts) {
    //取得 [檔案上傳].檔名
    var Tmp_FileObj = Ext.getCmp('FName');
    console.log("FName Obj:", Tmp_FileObj);
    var Tmp_Str = "";
    var np = {}
    np["FName"] = Ext.getDom(Tmp_FileObj.inputId).value;    
    console.log("FName: ", np.FName);
    //submit會將fileUploadForm裡面input name送到後端
    //Ext.getCmp('btn_FileIn').getForm().submit({
    me.up("form").submit({
        //standardSubmit: false,    //default:false
        url: '../api/V20304FAPI/uploadFileToDB',
        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('EX_DFile');
        //console.log("r_cookies=", r);
        if (!checkisnull(r)) {
            mysuccessalert(r);
            clearInterval(timer);
            mask.hide();
            timer = null;
            //將視窗關閉
            Ext.getCmp("SelectForm_cancelbtn").fireHandler();
        }
    }, 1000);  //1000ms = 1sec
};  // end of Ok_process() {  //批次新增

2>*.cs
         //[批次工時匯入] 鈕 , 匯入 XLS 檔案
        //因為傳送[錯誤訊息]檔案至前端, 所以不可以使用  HttpResponseMessage  response
        [HttpPost]
        public void uploadFileToDB()
        {

            HttpContext c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            string FName = nvc["FName"];

            MemoryStream mstream = new MemoryStream();
         // 取得實實的路徑            
            string documentPath = HttpContext.Current.Server.MapPath("~") + "document\\";  
            string FileName = "V20304_工時批次匯入_錯誤說明" + ".xlsx";
            string FileName1 = documentPath + FileName;
            HttpCookie MyCookie;  //將訊息傳至前端
        
            Stream file_Strm = Request.Files[cur_FName].InputStream;
            wk = new Workbook();
            wk.LoadFromStream(file_Strm);
            Worksheet sheet1 = wk.Worksheets[0];//獲取第一個工作表

            Tmp_OGONOAF = sheet1.Range[myfunc.GetExcelPos(0,i+1)].Value;
            sheet1.Range[myfunc.GetExcelPos(27, i + 1)].Value = Tmp_RtnMsg1;

          if (Tmp_isOk==false)
                    {
                        wk.SaveToStream(mstream, FileFormat.Version2007);
                        wk.SaveToFile(FileName1, FileFormat.Version2007);
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName1, 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());

                        //匯入檔案失敗
                        Tmp_Str = "匯入檔案失敗<br>"
                                        + "(" + FName + ")<br>";
                        MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
                        HttpContext.Current.Response.Cookies.Add(MyCookie);
                        HttpContext.Current.Response.End();
                        return;                   
                    }                                    

沒有留言:

張貼留言