顯示具有 f_downloadFile 標籤的文章。 顯示所有文章
顯示具有 f_downloadFile 標籤的文章。 顯示所有文章

2022年10月23日 星期日

V80C03 - 匯出目前畫面 store 的資料 - f_downloadFile -自己產生Form & submit,不會傳畫面欄位值

 目的: V80C03 - 匯出目前畫面  store 的資料

處理說明: 1>f_downloadFile 函式會自已產生 Form  & submit , 
                       並不會帶本子視窗畫面的 欄位值(Ex: chkboxgroup2)



1>*.js
 //匯出
    function xlsOutBtn_click() {
        var np = s_JSON('s_form'); //TMFunction.js, 組合form上的查詢條件為json參數傳遞
        np["FName"] = "V80C03_匯出.xlsx";

        console.log("np:", np);
        console.log("FName: ", np.FName);
        //EX_DFile : cookies, 傳回訊息  - 因傳回檔案不可用 HttpMessage , 所以用 Cookie 傳訊息
         //f_downloadFile 函式會自已產生 Form  & submit , 並不會帶本子視窗畫面的 欄位值(Ex: chkboxgroup2)
        f_downloadFile('EX_DFile', '../api/V80C03API/ExportdFile', np, 'POST', function () {
            var r = r_cookies('EX_DFile');
            if (!checkisnull(r)) 
                mysuccessalert(r);
        });
    };


2>*.cs
//V80C03  匯出 *.XLS 檔案  - 匯出

        [HttpPost]
        public void ExportdFile()
        {
            var c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;

            //匯出時, 不分頁(par_paging=false), 資料全取 
            DataSet ds = getGridData_M(false);

            string Tmp_FName = nvc["FName"];   //含 *.xls
            //string fName = Tmp_FNAME + ".xlsx";
            string fName = Tmp_FName;
            Workbook wk = null;
            MemoryStream stream = new MemoryStream();
            string Tmp_Str = "";
            int Tmp_Cnt = 0;
            HttpCookie MyCookie;
            try
            {

                wk = new Workbook();
                Worksheet sheet1 = wk.Worksheets[0];//獲取第一個工作表
                //共 13 欄
                //項次  日期(DT) PLANDCARD 領班 領班姓名 小組長 小組長姓名 小組成員(EMPLYID) 成員姓名 人(STAFF) 天(DURAION) 工時(STDHRS) 已報工時

                string[] outFieldArray = { "項次", "日期(DT)", "PLANDCARD", "領班", "領班姓名", "小組長", 
                                                           "小組長姓名", "小組成員(EMPLYID)", "成員姓名", "人(STAFF)" , "天(DURAION)",
                                                           "工時(STDHRS)", "已報工時"};
                for (int i = 0; i < outFieldArray.Length; i++)
                {
                    sheet1.Range[myfunc.GetExcelPos(i, 0)].Text = outFieldArray[i];
                }

                DataTable dt = ds.Tables[1];
                Tmp_Cnt = dt.Rows.Count;
                //Tmp_Str = "已匯出完成!!  ("+Tmp_Cnt.ToString() +"筆) ";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //共 13 欄
                    //項次  日期(DT) PLANDCARD 領班 領班姓名 小組長 小組長姓名 小組成員(EMPLYID) 成員姓名
                    //人(STAFF) 天(DURAION) 工時(STDHRS) 已報工時(ALS_HR_)
                    sheet1.Range[myfunc.GetExcelPos(0, i + 1)].Text = String.Format("{0:d}", i + 1);
                    if (!myfunc.checkisnull(dt.Rows[i]["DT"].ToString()))
                    { sheet1.Range[myfunc.GetExcelPos(1, i + 1)].Text = String.Format("{0:yyyy/MM/dd}", DateTime.Parse(dt.Rows[i]["DT"].ToString())); };
                    sheet1.Range[myfunc.GetExcelPos(2, i + 1)].Text = dt.Rows[i]["PLANNINGCARD"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(3, i + 1)].Text = dt.Rows[i]["FOREMAN1"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(4, i + 1)].Text = dt.Rows[i]["FOREMAN1_"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(5, i + 1)].Text = dt.Rows[i]["LDMAN1"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(6, i + 1)].Text = dt.Rows[i]["LDMAN1_"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(7, i + 1)].Text = dt.Rows[i]["EMPLYID"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(8, i + 1)].Text = dt.Rows[i]["EMPLYID_"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(9, i + 1)].Text = dt.Rows[i]["STAFF"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(10, i + 1)].Text = dt.Rows[i]["DURAION"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(11, i + 1)].Text = dt.Rows[i]["STDHRS"].ToString();
                    sheet1.Range[myfunc.GetExcelPos(12, i + 1)].Text = dt.Rows[i]["ALS_HR_"].ToString();
                    
                    // end of for 
                }  // end of   for (int i = 0; i < dt.Rows.Count; i++)
                sheet1.AllocatedRange.AutoFitColumns();
                wk.SaveToStream(stream, FileFormat.Version2007);
            } //end of try
            catch (Exception e)
            {
                //Console.WriteLine(e);
                //throw;
                var Tmp_ErrMsg = e.Message;
                //包裝標籤檔產生完成
                Tmp_Str = "匯出檔案失敗<br>"
                                + Tmp_ErrMsg;
                MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                return;
            }
            finally
            {
                wk.Dispose();
            }

            //Tmp_Cnt = 6;
           //將檔案傳送至前端 & 加入 Cookie 訊息
            Tmp_Str = "已匯出完成!!  (" + Tmp_Cnt.ToString() + "筆) <br>"
                            + "檔案名稱(" + Tmp_FName + ")";
            MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
            HttpContext.Current.Response.Cookies.Add(MyCookie);
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fName, System.Text.Encoding.UTF8) + "\"");
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.End();
        }    // end of ExportdFile