C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel
網(wǎng)上C#導(dǎo)出Excel的方法有很多。但用來用去感覺不夠自動化。于是花了點時間,利用特性做了個比較通用的導(dǎo)出方法。只需要根據(jù)實體類,自動導(dǎo)出想要的數(shù)據(jù)
1.在NuGet上安裝Aspose.Cells或者用微軟自帶類庫也可以
2.需要導(dǎo)出的數(shù)據(jù)的實例類:
using System.ComponentModel; using System.Reflection; using System.Runtime.Serialization; public class OrderReport { [DisplayName("訂單編號")] public string orderNo { get; set; } [IgnoreDataMember] public DateTime orderTime { get; set; } [DisplayName("訂單時間")] public String orderTime_fomart { get { return orderTime.ToShortDateString(); } } [DisplayName("商品編碼")] public string itemCode { get; set; } [DisplayName("商品名稱")] public string itemName { get; set; } }
定義實體中加上 [DisplayName("訂單編號")]用來導(dǎo)出到Excel生成列名。不需在導(dǎo)出一一對應(yīng)寫列名。[IgnoreDataMember]屬性是用來導(dǎo)出是忽略掉不用導(dǎo)出 。
關(guān)于特性的介紹詳細請參考MSDN。
3.實現(xiàn)導(dǎo)出方法:
/// <summary> /// 導(dǎo)出類 /// </summary> public class ExportHandle { /// <summary> /// 掛起訂單報表導(dǎo)出 /// </summary> public static void execExportOrderReport() { var orderReportList = new List<OrderReport>() { new OrderReport() { orderNo= "XD00001",orderTime=DateTime.Now, itemCode="G001" ,itemName="辣條"} , new OrderReport() { orderNo= "XD00002", orderTime=DateTime.Now,itemCode="G002" ,itemName="茶蛋"} , new OrderReport() { orderNo= "XD00003", orderTime=DateTime.Now,itemCode="G003" ,itemName="切糕"} , new OrderReport() { orderNo= "XD00004", orderTime=DateTime.Now,itemCode="G004" ,itemName="大蝦"} , new OrderReport() { orderNo= "XD00005", orderTime=DateTime.Now,itemCode="G005" ,itemName="帝王蟹"} }; string path = "OrderReport.xlsx"; Console.WriteLine("開始執(zhí)行導(dǎo)出"); OutDataToExcel(orderReportList, "訂單報表", path); Console.WriteLine("導(dǎo)出完成:位置"+path); } /// <summary> /// 導(dǎo)出方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">導(dǎo)出的數(shù)據(jù)list</param> /// <param name="title">數(shù)據(jù)類容標題</param> /// <param name="path">導(dǎo)出excel存放路徑</param> public static void OutDataToExcel<T>(List<T> list, string title, string path) { Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 sheet.IsGridlinesVisible = false;//去掉初始單元線 Cells cells = sheet.Cells;//單元格 //為標題設(shè)置樣式 Style styleTitle = workbook.CreateStyle();//新增樣式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 styleTitle.Font.Name = "微軟雅黑";//文字字體 styleTitle.Font.Size = 18;//文字大小 styleTitle.Font.IsBold = true;//粗體 //樣式1 標題下方的日期 Style style1 = workbook.CreateStyle();//新增樣式 style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style1.Font.Name = "微軟雅黑";//文字字體 style1.Font.Size = 12;//文字大小 //樣式2 列名 Style style2 = workbook.CreateStyle();//新增樣式 style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style2.Font.Name = "微軟雅黑";//文字字體 style2.Font.Size = 12;//文字大小 style2.Font.IsBold = true;//粗體 style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //樣式3 數(shù)據(jù)的樣式 Style style3 = workbook.CreateStyle();//新增樣式 style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style3.Font.Name = "微軟雅黑";//文字字體 style3.Font.Size = 10;//文字大小 style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; if (list.Count == 0) return; var t = list.First().GetType();//獲取列表的類的屬性 //通過反射篩選忽略掉[IgnoreDataMemberAttribute]的字段 var properties = t.GetProperties().Where(x => x.GetCustomAttribute<IgnoreDataMemberAttribute>() == null); int Colnum = properties.Count();//表格列數(shù) int Rownum = list.Count;//表格行數(shù) //生成行1 標題行 cells.Merge(0, 0, 1, Colnum);//合并單元格 cells[0, 0].PutValue(title);//填寫內(nèi)容 cells[0, 0].SetStyle(styleTitle); cells.SetRowHeight(0, 38);//行高 //生成行2 日期 cells.Merge(1, 0, 1, Colnum);//合并單元格 cells[1, 0].PutValue(DateTime.Now.ToShortDateString());//填寫內(nèi)容 cells[1, 0].SetStyle(style1); cells.SetRowHeight(1, 20);//行高 //列名及數(shù)據(jù)行 int i = 0; foreach (var item in properties) { var itemType = t.GetProperty(item.Name); var colName = itemType.GetCustomAttribute<DisplayNameAttribute>().DisplayName;//反射獲取字段的DisplayName特性值 cells[2, i].PutValue(colName); cells[2, i].SetStyle(style2); cells.SetColumnWidth(i, colName.Length * 3);//設(shè)置列寬 int k = 0; foreach (var rowdata in list) { //反射遍歷添加數(shù)據(jù) object value = rowdata.GetType().GetProperty(item.Name).GetValue(rowdata, null); string ss = value == null ? "" : value.ToString(); cells[3 + k, i].PutValue(ss); cells[3 + k, i].SetStyle(style3); cells.SetRowHeight(3 + k, 18);//設(shè)置行高 k++; } i++; } workbook.Save(path);//生成Excel } }
導(dǎo)出方法 OutDataToExcel<T>(List<T> list, Enum en, string path)用了泛型參數(shù),將任意的實體list自動導(dǎo)出。
var properties = t.GetProperties().Where(x => AttributeAccessor.GetAttribute<IgnoreDataMemberAttribute>(x) == null);
采用lamda表達式在傳過來的實體屬性中篩選出
不是IgnoreDataMemberAttribute的屬性字段
foreach (var item in properties){}遍歷實體類的屬性相當(dāng)于DataTable循環(huán)讀取數(shù)據(jù) object value = rowdata.GetType().GetProperty(item.Name).GetValue(rowdata, null); 通過屬性名稱獲取屬性值。 通過以上兩個步驟,實現(xiàn)自動 }
4.導(dǎo)出結(jié)果:
總結(jié),通過特性來實現(xiàn)通用的導(dǎo)出。只需要設(shè)置相關(guān)的類的字段和特性值即可自定義導(dǎo)出
以上所述是小編給大家介紹的C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:C#實現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼
欄 目:C#教程
下一篇:基于C#技術(shù)實現(xiàn)身份證識別功能
本文標題:C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6339.html
您可能感興趣的文章
- 01-10C#一個簡單的定時小程序?qū)崿F(xiàn)代碼
- 01-10微信開放平臺之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#調(diào)用WebService實例開發(fā)
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
- 01-10C#編程自學(xué)之開篇介紹
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
- 01-10C#編程自學(xué)之運算符和表達式
- 01-10實現(xiàn)ASP.NET無刷新下載并提示下載完成的開發(fā)思路
- 01-10C#編程自學(xué)之類和對象
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量一


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改