C#实现导出Excel模板

版权声明:本文为博主原创文章,转载时请注明出处。 https://blog.csdn.net/nxw_tsp/article/details/85092557

        /// <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("导出成功");
            }

猜你喜欢

转载自blog.csdn.net/nxw_tsp/article/details/85092557