国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

反射+屬性標簽 通用Excel導入導

2019-11-17 02:49:22
字體:
來源:轉載
供稿:網友

反射+屬性標簽 通用Excel導入導

在做通用導入導出的時候,最關鍵的應該就是實體導出導入的順序了,但是編譯器在編譯的時候又無法自定義屬性編譯的順序,所以需要一個自定義的特性標簽來指定實體類導出的順序,然后通過自定義的比較器將屬性排序

因為wcf中無法對實體類的自定義特性進行描述,所以獲取不到我們自定義的OrderAttribute,雖然DataMemberAttribute中的Order屬性是描述屬性序列化的順序,但是因為沒有對序列化排序沒有特殊的要求,于是就用它代替了,起初發射之后總是倒數兩個Order屬性的值是正常的,其他的都為-1,后來發現生成的順序也是按Order生成的,于是就沒有深究了(如果有深入研究的朋友 希望指點一下)。

class PRogram    {        static void Main(string[] args)        {            //過濾掉沒有打排序標簽的屬性            List<PropertyInfo> pis = typeof(People).GetProperties().Where(p => p.GetCustomAttributes(typeof(OrderAttribute), false).Any()).ToList();            //自定義比較器排序            pis.Sort(new OrderComparator());            Console.ReadKey();        }    }    //自定義排序特性    public class OrderAttribute : Attribute    {        public OrderAttribute(int order)        {            this.PropertyOrder = order;        }        public int PropertyOrder { get; set; }    }    //實體類    public class People    {        public int ID { get; set; }        [Order(1)]        public string Name { get; set; }        [Order(3)]        public int Age { get; set; }        [Order(2)]        public bool Gender { get; set; }        [Order(5)]        public double Height { get; set; }        [Order(4)]        public double Weight { get; set; }    }    //自定義屬性比較器    public class OrderComparator : IComparer<PropertyInfo>    {        public int Compare(PropertyInfo x, PropertyInfo y)        {            OrderAttribute xOrderAttribute = x.GetCustomAttributes(typeof(OrderAttribute), false).FirstOrDefault() as OrderAttribute;            OrderAttribute yOrderAttribute = y.GetCustomAttributes(typeof(OrderAttribute), false).FirstOrDefault() as OrderAttribute;            return xOrderAttribute.PropertyOrder - yOrderAttribute.PropertyOrder;        }    }

目前使用過操作excel的方式有NPOI和,微軟提供的Microsoft.Office.Interop.Excel性能太牛X了,所以就不敢貼出來了

一、使用NPOI

NPOI導出:

/// <summary>        /// 保存到硬盤        /// </summary>        /// <param name="path">保存路徑@"c:/book1.xls"</param>        /// <param name="data">數據源</param>        /// <param name="columnsName">excel列名</param>        public void SaveToFile<T>(List<T> data, string path, List<string> excelColumnsTitle)        {            if (Path.GetExtension(path).Equals(".xls"))            {                //excel2003                SaveToFile2003<T>(data, path, excelColumnsTitle);            }            else if (Path.GetExtension(path).Equals(".xlsx"))            {                //excel2007                SaveToFile2007<T>(data, path, excelColumnsTitle);            }            else            {                throw new Exception("請傳入正確的excel路徑");            }        }

  SaveToFile2003

 /// <summary>        /// excel2003導出        /// </summary>        private void SaveToFile2003<T>(List<T> data, string path, List<string> excelColumnsTitle)        {            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet"); //添加一個sheet            //給sheet1添加第一行的頭部標題            NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0);            for (int i = 0; i < excelColumnsTitle.Count; i++)            {                row1.CreateCell(i).SetCellValue(excelColumnsTitle[i]);            }            //過濾屬性            List<PropertyInfo> pis = typeof(T).GetProperties().Where(p => p.GetCustomAttributes(typeof(OrderAttribute), false).Any()).ToList();            pis.Sort(new OrderComparator());            //金額格式            NPOI.SS.UserModel.ICellStyle cellStyleDecimal = book.CreateCellStyle();            NPOI.SS.UserModel.IDataFormat formatDecimal = book.CreateDataFormat();            cellStyleDecimal.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0.00"); //單元格格式為“0.00”來表示,"¥#,##0"美元顯示,"0.00%"百分比顯示              //日期格式            NPOI.SS.UserModel.ICellStyle cellStyleDateTime = book.CreateCellStyle();            NPOI.SS.UserModel.IDataFormat formatDateTime = book.CreateDataFormat();            cellStyleDateTime.DataFormat = formatDateTime.GetFormat("yyyy-m");            //將數據逐步寫入sheet1各個行            for (int i = 0; i < data.Count; i++)            {                NPOI.SS.UserModel.IRow rowtemp = sheet.CreateRow(i + 1);                for (int j = 0; j < pis.Count; j++)                {                    NPOI.SS.UserModel.ICell cell = rowtemp.CreateCell(j);                    if (pis[j].PropertyType.IsAssignableFrom(typeof(string)))                    {                        cell.SetCellValue(pis[j].GetValue(data[i], null).ToString());                    }                    else if (pis[j].PropertyType.IsAssignableFrom(typeof(int)))                    {                        cell.SetCellValue(Convert.ToInt32(pis[j].GetValue(data[i], null)));                    }                    else if (pis[j].PropertyType.IsAssignableFrom(typeof(decimal)))                    {                        cell.CellStyle = cellStyleDecimal;                        cell.SetCellValue(Convert.ToDouble(pis[j].GetValue(data[i], null)));                    }                    else if (pis[j].PropertyType.IsAssignableFrom(typeof(DateTime)))                    {                        cell.CellStyle = cellStyleDateTime;                        cell.SetCellValue(Convert.ToDateTime(pis[j].GetValue(data[i], null)));                    }                    else if (pis[j].PropertyType.IsAssignableFrom(typeof(double)))                    {                        cell.CellStyle = cellStyleDecimal;                        cell.SetCellValue(Convert.ToDouble(pis[j].GetValue(data[i], null)));                    }                    else if (pis[j].PropertyType.IsAssignableFrom(typeof(bool)))                    {                        cell.SetCellValue(Convert.ToBoolean(pis[j].GetValue(data[i], null)));                    }                }            }            // 寫入到客戶端             using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, Fileaccess.Write))            {                book.Write(fs);            }        }

  SaveToFile2007

 /// <summary>        /// excel2007導出        /// </summary>        private void SaveToFile2007<T>(List<T> data, string path, List<string> excelColumnsTitle)        {            NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook();            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet"); //添加一個sheet            //給sheet1添加第一行的頭部標題            NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0);            for (int i = 0; i < excelColumnsTitle.Count; i++)            {                row1.CreateCell(i).SetCellValue(excelColumnsTitle[i]);            }            //過濾屬性            List<PropertyInfo> pis = typeof(T).GetProperties().Where(p => p.GetCustomAttributes(typeof(OrderAttribute), false).Any()).ToList();            pis.Sort(new OrderComparator());            //金額格式            NPOI.SS.UserModel.ICellStyle cellStyleDecimal = book.CreateCellStyle();            NPOI.SS.UserModel.IDataFormat formatDecimal = book.CreateDataFormat();            cellStyleDecimal.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0.00"); //單元格格式為“0.00”來表示,"¥#,##0"美元顯示,"0.00%"百分比顯示              //日期格式            NPOI.SS.UserModel.ICellStyle cellStyleDateTime = book.CreateCellStyle();            NPOI.SS.UserModel.IDataFormat formatDateTime = book.CreateDataFormat();            cellStyleDateTime.DataFormat = formatDateTime.GetFormat("yyyy-m");            //將數據逐步寫入sheet1各個行            for (int i = 0; i < data.Count; i++)            {                NPOI.SS.UserModel.IRow rowtemp = sheet.CreateRow(i + 1);                for (int j = 0; j < pis.Count; j++)                {                    NPOI.SS.UserModel.ICell cell = r
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昆山市| 城步| 朝阳县| 上林县| 永福县| 房产| 云龙县| 普洱| 抚顺市| 蓬安县| 垫江县| 乐都县| 斗六市| 无锡市| 连江县| 阳朔县| 天柱县| 宜阳县| 鹤岗市| 喀喇| 芜湖市| 合作市| 宜兰市| 区。| 肃北| 留坝县| 彩票| 宝山区| 华安县| 新泰市| 宣恩县| 新丰县| 泌阳县| 玉山县| 遂昌县| 南华县| 泽库县| 攀枝花市| 榕江县| 灌阳县| 邻水|