• 封装Excls数据导出功能 返回一个下载链接地址


     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 TransitionHelper
        {
            private static volatile TransitionHelper drugParamsHelper = null;
            private static object syncRoot = new Object();
            public TransitionHelper()
            {
    
            }
            /// <summary>
            /// 初始化注册
            /// </summary>
            public static TransitionHelper Instance
            {
                get
                {
                    if (drugParamsHelper == null)
                    {
                        lock (syncRoot)
                        {
                            if (drugParamsHelper == null)
                            {
                                drugParamsHelper = new TransitionHelper();
                            }
                        }
                    }
                    return drugParamsHelper;
                }
            }
            /// <summary>
            /// 获取本地存储地址
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="prefix">前缀类别</param>
            /// <returns></returns>
            public string GetDwonLoadlPath<T>(List<T> list,Dictionary<string,string> dic, string path)
            {
                DataTable dataTable = ListTranDataTableHelper.ToDataTable(list, dic); 
                return GetDwonLoadlPath(dataTable, path);
            }
    
            public string GetDwonLoadlPath(DataTable dt, string path)
            {
                string Domain = AppsettingsHelper.app(new string[] { "ApplicationDomain" }).ObjToString();
                string subpath = @"/Export/";
                string fullpath = path + subpath;
                DicCreate(fullpath);
                string filename = Guid.NewGuid() + ".xlsx";
                string fullfilepath = fullpath + filename;
                EplusExcleHelper.DataTableToExcel(dt, fullfilepath);
                return Domain+subpath+filename;
            }
    
            /// <summary>
            /// 文件目录如果不存在,就创建一个新的目录
            /// </summary>
            /// <param name="path"></param>
            private void DicCreate(string path)
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

    调用示例 

    string filepath = TransitionHelper.Instance.GetDwonLoadlPath(list, RenameColumDic, webRootPath); 
    //转换列名称成中文名 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"));
  • 相关阅读:
    Python3中zipfile模块文件名乱码问题
    python zipfile 文件压缩和文件
    django+celery+rabitmq
    解决 Jumpserver coco 使用登录用户(ldap)进行SSH连接目标主机,忽略系统用户
    日志实时查看、轮询、统计、监控工具 Loggrove
    python django 多级业务树形结构规划及页面渲染
    nginx+uwsgi 部署 django
    python tornado websocket 实时日志展示
    python tornado websocket 多聊天室(返回消息给部分连接者)
    python 实时遍历日志文件
  • 原文地址:https://www.cnblogs.com/zzlblog/p/10013164.html
Copyright © 2020-2023  润新知