1. 程式人生 > >C#實現匯出Excel模板

C#實現匯出Excel模板


        /// <summary>
        /// 匯出模板
        /// </summary>
        /// <returns></returns>
        [Action]
        public IActionResult Export(string k)
        {
                List<Outlet> ds = new List<Outlet>();
                Outlet kk = new Outlet();
                ds.Add(kk);
                List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>();
                keyValues.Add(new KeyValuePair<string, string>("Region", "Region"));
                keyValues.Add(new KeyValuePair<string, string>("DC", "DC"));
                keyValues.Add(new KeyValuePair<string, string>("CustomerCode", "Customer code(賬號)"));
                keyValues.Add(new KeyValuePair<string, string>("KA", "KA"));
                keyValues.Add(new KeyValuePair<string, string>("OutletCode", "客戶outlet code"));
                keyValues.Add(new KeyValuePair<string, string>("NestleOutletCode", "NestleOutletCode"));
                keyValues.Add(new KeyValuePair<string, string>("Remark", "Remark"));
                string fileName = "Outlet Mapping.xlsx";
                MemoryStream ms = Excel.ExportToExcelStream<Outlet>(fileName, ds, keyValues, "Outlet Mapping");
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).Replace("+", "%20"));
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                HttpContext.Current.Response.End();
                ms.Close();
                ms = null;
                return ApiResult.Success("匯出成功");
            }