码迷,mamicode.com
首页 > 其他好文 > 详细

封装Excls数据导出功能 返回一个文件链接地址

时间:2018-11-24 20:56:21      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:environ   xlsx   obj   stat   DLP   splay   web   odata   system   

 1    /// <summary>
 2         /// 获取本地存储地址
 3         /// </summary>
 4         /// <param name="dt"></param>
 5         /// <param name="prefix">前缀类别</param>
 6         /// <returns></returns>
 7         public string GetDwonLoadlPath(DataTable dt, string prefix)
 8         {
 9             string filename = System.Configuration.ConfigurationManager.AppSettings[prefix].ToString() + Guid.NewGuid() + ".xlsx";
10             string filePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "\\File\\Download\\" + filename;
11             string returnpath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/File/Download/" + filename;
12             ExcelHelper.DataTableToExcel(dt, filePath);
13             return System.Configuration.ConfigurationManager.AppSettings["DomainName"] + returnpath;
14         }
 public class DrugParamsHelper
    {
        private static volatile DrugParamsHelper drugParamsHelper = null;
        private static object syncRoot = new Object();
        public DrugParamsHelper()
        {
         
        }
        /// <summary>
        /// 初始化注册
        /// </summary>
        public static DrugParamsHelper Instance
        {
            get
            {
                if (drugParamsHelper == null)
                {
                    lock (syncRoot)
                    {
                        if (drugParamsHelper == null)
                        {
                            if (true)
                            {
                            }
                            drugParamsHelper = new DrugParamsHelper();
                        }
                    }
                }
                return drugParamsHelper;
            }
        }
        /// <summary>
        /// 获取本地存储地址
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="prefix">前缀类别</param>
        /// <returns></returns>
        public string GetDwonLoadlPath(DataTable dt, string prefix)
        {
            string filename = System.Configuration.ConfigurationManager.AppSettings[prefix].ToString() + Guid.NewGuid() + ".xlsx";
            string filePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "\\File\\Download\\" + filename;
            string returnpath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/File/Download/" + filename;
            ExcelHelper.DataTableToExcel(dt, filePath);
            return System.Configuration.ConfigurationManager.AppSettings["DomainName"] + returnpath;
        }}

调用示例 

  //转换列名称成中文名
            Dictionary<string, string> RenameColumDic = new Dictionary<string, string>();
            queryParams.colCollection.ForEach(f =>
            {
                RenameColumDic.Add(f.FieldName, f.DisplayName);
            });
            //将对象转成datatble
            DataTable dataTable = ListTranDataTableHelper.ToDataTable<ClinicalRegistrationCach>(list, RenameColumDic, queryParams.colCollection.Select(c => c.FieldName).ToArray());
            return new Response<string>(DrugParamsHelper.Instance.GetDwonLoadlPath(dataTable, "CDEExclsName"));

 

封装Excls数据导出功能 返回一个文件链接地址

标签:environ   xlsx   obj   stat   DLP   splay   web   odata   system   

原文地址:https://www.cnblogs.com/zzlblog/p/10013164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!