Exemplo n.º 1
0
        public DBsettings()
        {
            InitializeComponent();

                        /*
                        SkinningManager sm1 = new SkinningManager();
                        sm1.ParentForm = this;
                        sm1.DefaultSkin = DefaultSkin.Office2007Silver;
                        */

                        // 读取已经有的配置
                        m_path = new MyPath();
                        m_strConfigPath = m_path.GetSystem32Path();
                        m_strConfigPath += "\\saito.xml";
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据IEnumerable T类型的数据导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="folder"></param>
        /// <param name="list"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string Export <T>(string folder, IEnumerable <T> list, string name) where T : new()
        {
            //string folder = _hostingEnvironment.WebRootPath;

            var t = typeof(T);
            //var displayNameOfT = t.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
            //var displayNameOfT = string.Join(",", displayNameListOfT.Select(i => i.DisplayName));
            //if (string.IsNullOrWhiteSpace(displayNameOfT)) displayNameOfT = "未指定类型显示名称";
            //var pName = t.GetProperty("Name");
            //var displayName = pName.GetCustomAttribute<DisplayNameAttribute>();

            //var displayNameOfT = MyPath.Combine("excel", name);

            //string sFileName = $"{name}{Guid.NewGuid()}.xlsx";
            string sFileName = $"{name}{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            //string sFileName = $"{name}.xlsx";
            FileInfo file = new FileInfo(MyPath.Combine(folder, sFileName));

            using (ExcelPackage package = new ExcelPackage(file))
            {
                // 添加worksheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(name);

                var propertys = t.GetProperties();
                for (int i = 0; i < propertys.Count(); i++)
                {
                    var displayName = propertys[i].GetCustomAttribute <DisplayNameAttribute>()?.DisplayName;
                    if (string.IsNullOrWhiteSpace(displayName) || displayName == "未指定")
                    {
                        continue;
                    }
                    worksheet.Cells[1, i + 1].Value = displayName;

                    //获取DisplayFormatAttribute的DataFormatString
                    //var displayFormat = propertys[i].GetCustomAttribute<DisplayFormatAttribute>()?.DataFormatString;

                    var propertyName = propertys[i].Name;
                    for (int j = 0; j < list.Count(); j++)
                    {
                        var item  = list.Skip(j).FirstOrDefault();
                        var value = item.GetType().GetProperty(propertyName).GetValue(item)?.ToString();
                        //var value = item.GetType().GetProperty(propertyName).GetValue(item);
                        //var displayValue = displayFormat==null ? value : string.Format( displayFormat,value);
                        //worksheet.Cells[j + 2, i + 1].Value = displayValue;

                        //2018/2/26 写死了有"金额"字样则做特定格式化.应该根据sql的额外描述,获取DisplayFormatAttribute的DataFormatString,然后由propertys[i](确定是哪种数值或datetime,别的不行)和DataFormatString来使用string.Format格式化.c#的反射其实很烦又臃肿.
                        //if (displayName.Contains("金额"))
                        //{
                        //    value = string.Format("{0:C2}", Convert.ToInt32(value));
                        //}

                        worksheet.Cells[j + 2, i + 1].Value = value;
                    }
                }

                ////添加头
                //worksheet.Cells[1, 1].Value = "ID";
                //worksheet.Cells[1, 2].Value = "Name";
                //worksheet.Cells[1, 3].Value = "Url";
                ////添加值
                //worksheet.Cells["A2"].Value = 1000;
                //worksheet.Cells["B2"].Value = "LineZero";
                //worksheet.Cells["C2"].Value = "http://www.cnblogs.com/linezero/";

                //worksheet.Cells["A3"].Value = 1001;
                //worksheet.Cells["B3"].Value = "LineZero GitHub";
                //worksheet.Cells["C3"].Value = "https://github.com/linezero";
                //worksheet.Cells["C3"].Style.Font.Bold = true;


                package.Save();
            }

            return(sFileName);
            //return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }