欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel

 更新時(shí)間:2016年07月26日 11:19:23   作者:costyuan  
這篇文章主要介紹了C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel的相關(guān)資料,需要的朋友可以參考下

網(wǎng)上C#導(dǎo)出Excel的方法有很多。但用來用去感覺不夠自動(dòng)化。于是花了點(diǎn)時(shí)間,利用特性做了個(gè)比較通用的導(dǎo)出方法。只需要根據(jù)實(shí)體類,自動(dòng)導(dǎo)出想要的數(shù)據(jù)

1.在NuGet上安裝Aspose.Cells或者用微軟自帶類庫(kù)也可以

2.需要導(dǎo)出的數(shù)據(jù)的實(shí)例類:

using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
public class OrderReport
{
[DisplayName("訂單編號(hào)")]
public string orderNo { get; set; }
[IgnoreDataMember]
public DateTime orderTime { get; set; }
[DisplayName("訂單時(shí)間")]
public String orderTime_fomart { get { return orderTime.ToShortDateString(); } }
[DisplayName("商品編碼")]
public string itemCode { get; set; }
[DisplayName("商品名稱")]
public string itemName { get; set; }
} 

定義實(shí)體中加上 [DisplayName("訂單編號(hào)")]用來導(dǎo)出到Excel生成列名。不需在導(dǎo)出一一對(duì)應(yīng)寫列名。[IgnoreDataMember]屬性是用來導(dǎo)出是忽略掉不用導(dǎo)出 。

關(guān)于特性的介紹詳細(xì)請(qǐng)參考MSDN。

3.實(shí)現(xiàn)導(dǎo)出方法:

/// <summary>
/// 導(dǎo)出類
/// </summary>
public class ExportHandle
{
/// <summary>
/// 掛起訂單報(bào)表導(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, "訂單報(bào)表", 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ù)類容標(biāo)題</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;//單元格 
//為標(biāo)題設(shè)置樣式 
Style styleTitle = workbook.CreateStyle();//新增樣式 
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
styleTitle.Font.Name = "微軟雅黑";//文字字體 
styleTitle.Font.Size = 18;//文字大小 
styleTitle.Font.IsBold = true;//粗體 
//樣式1 標(biāo)題下方的日期
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 標(biāo)題行 
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ù),將任意的實(shí)體list自動(dòng)導(dǎo)出。

var properties = t.GetProperties().Where(x => AttributeAccessor.GetAttribute<IgnoreDataMemberAttribute>(x) == null);

采用lamda表達(dá)式在傳過來的實(shí)體屬性中篩選出

不是IgnoreDataMemberAttribute的屬性字段

foreach (var item in properties){}遍歷實(shí)體類的屬性相當(dāng)于DataTable循環(huán)讀取數(shù)據(jù) 
object value = rowdata.GetType().GetProperty(item.Name).GetValue(rowdata, null); 通過屬性名稱獲取屬性值。
通過以上兩個(gè)步驟,實(shí)現(xiàn)自動(dòng)
}

4.導(dǎo)出結(jié)果:

總結(jié),通過特性來實(shí)現(xiàn)通用的導(dǎo)出。只需要設(shè)置相關(guān)的類的字段和特性值即可自定義導(dǎo)出

以上所述是小編給大家介紹的C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • C#中的日期時(shí)間比較和格式化的方法

    C#中的日期時(shí)間比較和格式化的方法

    本文將介紹C#中常用的日期時(shí)間比較方法(CompareTo、Equals和比較運(yùn)算符)以及日期時(shí)間格式化方法(ToString、自定義格式字符串和標(biāo)準(zhǔn)格式),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • C#中的預(yù)定義類型與引用類型

    C#中的預(yù)定義類型與引用類型

    這篇文章介紹了C#中的預(yù)定義類型與引用類型,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • Unity代碼實(shí)現(xiàn)序列幀動(dòng)畫播放器

    Unity代碼實(shí)現(xiàn)序列幀動(dòng)畫播放器

    這篇文章主要為大家詳細(xì)介紹了Unity代碼實(shí)現(xiàn)序列幀動(dòng)畫播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#關(guān)于Func和Action委托的介紹詳解

    C#關(guān)于Func和Action委托的介紹詳解

    委托是存有對(duì)某個(gè)方法的引用的一種引用類型變量,本文主要介紹了C#關(guān)于Func和Action委托的介紹,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#中while循環(huán)語(yǔ)句用法實(shí)例詳解

    C#中while循環(huán)語(yǔ)句用法實(shí)例詳解

    這篇文章主要介紹了C#中while循環(huán)語(yǔ)句用法,以實(shí)例形式詳細(xì)分析了while語(yǔ)句的用法,并對(duì)return,continue,break的區(qū)別做了進(jìn)一步的分析,需要的朋友可以參考下
    2014-10-10
  • WPF自定義實(shí)現(xiàn)上傳文件顯示進(jìn)度的按鈕控件

    WPF自定義實(shí)現(xiàn)上傳文件顯示進(jìn)度的按鈕控件

    自定義控件在WPF開發(fā)中是很常見的,有時(shí)候某些控件需要契合業(yè)務(wù)或者美化統(tǒng)一樣式,這時(shí)候就需要對(duì)控件做出一些改造,本文就來自定義實(shí)現(xiàn)一個(gè)上傳文件顯示進(jìn)度的按鈕控件吧
    2023-06-06
  • Unity?UGUI的Canvas畫布組件使用示例詳解

    Unity?UGUI的Canvas畫布組件使用示例詳解

    這篇文章主要介紹了Unity?UGUI的Canvas畫布組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • C#借助OpenCvSharp讀取攝像頭并顯示的實(shí)現(xiàn)示例

    C#借助OpenCvSharp讀取攝像頭并顯示的實(shí)現(xiàn)示例

    OpenCvSharp是一個(gè)OpenCV的.Net wrapper,應(yīng)用最新的OpenCV庫(kù)開發(fā),本文主要介紹了C#借助OpenCvSharp讀取攝像頭并顯示的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-05-05
  • 基于C#制作一個(gè)休息提醒鬧鐘的詳細(xì)步驟

    基于C#制作一個(gè)休息提醒鬧鐘的詳細(xì)步驟

    小鬧鐘大家都應(yīng)該很熟悉,它包括時(shí)間、事件,當(dāng)達(dá)到某某時(shí)間時(shí),事件發(fā)生了,并且還有一個(gè)提示信息,下面這篇文章主要給大家介紹了關(guān)于如何基于C#制作一個(gè)休息提醒鬧鐘的詳細(xì)步驟,需要的朋友可以參考下
    2023-02-02
  • asp.net中調(diào)用oracle存儲(chǔ)過程的方法

    asp.net中調(diào)用oracle存儲(chǔ)過程的方法

    存儲(chǔ)過程是在大型數(shù)據(jù)庫(kù)系統(tǒng)中,一組為了完成特定功能的SQL 語(yǔ)句集,存儲(chǔ)在數(shù)據(jù)庫(kù)中經(jīng)過第一次編譯后再次調(diào)用不需要再次編譯,用戶通過指定存儲(chǔ)過程的名字并給出參數(shù)來執(zhí)行它,下面給大家介紹下asp.net中調(diào)用oracle存儲(chǔ)過程的方法,需要的朋友可以參考下
    2015-08-08

最新評(píng)論