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

C#使用NPOI導(dǎo)出Excel類封裝

 更新時(shí)間:2022年02月21日 16:37:02   作者:武尚發(fā)的博客  
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI導(dǎo)出Excel類封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒(méi)有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作。 NPOI是一個(gè)開(kāi)源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項(xiàng)目。

以下代碼主要分3部分:通過(guò)實(shí)體類的自定義特性導(dǎo)出Excel文件

1、封裝類:ExcelHelper
2、實(shí)體類:StudentModel
3、調(diào)用:Form1

ExcelHelper

?/// <summary>
/// Excel操作類
/// </summary>
/// <typeparam name="T">實(shí)體類</typeparam>
public class ExcelHelper<T> where T : class
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 保存Excel文件
? ? ? ? /// </summary>
? ? ? ? /// <param name="excelName">Excel文件名</param>
? ? ? ? /// <param name="sheetName">Sheet工作表名</param>
? ? ? ? /// <param name="data">實(shí)體類對(duì)象</param>
? ? ? ? public static void SaveExcelFile(string excelName, string sheetName, List<T> data)
? ? ? ? {

? ? ? ? ? ? IWorkbook workBook = new HSSFWorkbook(); //創(chuàng)建一個(gè)Excel文檔
? ? ? ? ? ? ISheet sheet = workBook.CreateSheet(sheetName); //創(chuàng)建一個(gè)工作表Sheet


? ? ? ? ? ? int rowNum = 0;
? ? ? ? ? ? var row = sheet.CreateRow(sheet.LastRowNum); //LastRowNum記錄當(dāng)前可用寫入的行索引
? ? ? ? ? ? PropertyInfo[] preInfo = typeof(T).GetProperties();//獲取這個(gè)實(shí)體對(duì)象的所有屬性
? ? ? ? ? ? foreach (var item in preInfo)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? object[] objPres = item.GetCustomAttributes(typeof(DescriptionAttribute), true);//獲取當(dāng)前屬性的自定義特性列表
? ? ? ? ? ? ? ? if (objPres.Length > 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < objPres.Length; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? row.CreateCell(rowNum).SetCellValue(((DescriptionAttribute)objPres[i]).Description);//創(chuàng)建行,將當(dāng)前自定義特性寫入
? ? ? ? ? ? ? ? ? ? ? ? rowNum++;//行索引加1,下次往后一格創(chuàng)建行
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }


? ? ? ? ? ? int j = sheet.LastRowNum + 1, columnNum = 0;
? ? ? ? ? ? foreach (var item in data)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? columnNum = 0;
? ? ? ? ? ? ? ? row = sheet.CreateRow(j++);

? ? ? ? ? ? ? ? var itemProps = item.GetType().GetProperties(); ?//獲取當(dāng)前對(duì)象的屬性列表
? ? ? ? ? ? ? ? foreach (var itemPropSub in itemProps)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //獲取當(dāng)前對(duì)象特性中的自定義特性[Description("自定義特性")]
? ? ? ? ? ? ? ? ? ? var objs = itemPropSub.GetCustomAttributes(typeof(DescriptionAttribute), true);
? ? ? ? ? ? ? ? ? ? if (objs.Length > 0)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //將當(dāng)前對(duì)象的特性值,插入當(dāng)前行的第n列單元格
? ? ? ? ? ? ? ? ? ? ? ? row.CreateCell(columnNum).SetCellValue(itemPropSub.GetValue(item, null) == null ? "" : itemPropSub.GetValue(item, null).ToString());
? ? ? ? ? ? ? ? ? ? ? ? columnNum++;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? //文件流寫入
? ? ? ? ? ? using (MemoryStream ms = new MemoryStream())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? workBook.Write(ms);
? ? ? ? ? ? ? ? using (FileStream fs = new FileStream(excelName, FileMode.Create, FileAccess.Write))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ms.WriteTo(fs);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ms.Flush();
? ? ? ? ? ? ? ? ms.Position = 0;
? ? ? ? ? ? ? ? workBook.Close();
? ? ? ? ? ? }
? ? ? ? }
? ? }

StudentModel

//實(shí)體類
public class StudentModel
? ? {
? ? ? ? [Description("學(xué)號(hào)")]
? ? ? ? public string ID { get; set; }
? ? ? ? [Description("姓名")]
? ? ? ? public string Name { get; set; }
? ? ? ? [Description("年齡")]
? ? ? ? public string Age { get; set; }
? ? ? ? [Description("性別")]
? ? ? ? public string Six { get; set; }
? ? ? ? [Description("地址")]
? ? ? ? public string Address { get; set; }
? ? ? ? [Description("電話")]
? ? ? ? public string Tel { get; set; }
? ? }

調(diào)用:

List<StudentModel> stdList = new List<StudentModel>();
stdList.Add(new StudentModel() { ID = "001", Name = "Peter", Age = "26", Six = "男", Address = "東京", Tel = "123456789" });
stdList.Add(new StudentModel() { ID = "002", Name = "Jerry", Age = "36", Six = "男", Address = "首爾", Tel = "987654321" });
ExcelHelper<StudentModel>.SaveExcelFile(Application.StartupPath + "\\StudentInfo.xls", "Student", stdList);

結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# wpf解決Popup彈出位置異常問(wèn)題解決

    C# wpf解決Popup彈出位置異常問(wèn)題解決

    本文主要介紹了C# wpf解決Popup彈出位置異常問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲

    C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】

    C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】

    這篇文章主要介紹了C#實(shí)現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實(shí)例形式分析了Gzip和Zip兩種壓縮操作實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-06-06
  • C#創(chuàng)建線程帶參數(shù)的方法

    C#創(chuàng)建線程帶參數(shù)的方法

    本文給大家介紹C#創(chuàng)建線程帶參數(shù)的方法,包括無(wú)參數(shù)線程的創(chuàng)建,帶一個(gè)參數(shù)線程的創(chuàng)建及帶兩個(gè)及以上參數(shù)線程的創(chuàng)建,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-07-07
  • C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法示例總結(jié)

    C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法示例總結(jié)

    這篇文章主要介紹了C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法,結(jié)合實(shí)例形式總結(jié)分析了C#通過(guò)反射、轉(zhuǎn)換等方法讀取匿名對(duì)象屬性值的相關(guān)操作技巧,需要的朋友可以參考下
    2020-03-03
  • C#讀寫文件的方法匯總

    C#讀寫文件的方法匯總

    C#讀寫文件的方法匯總,需要的朋友可以參考一下
    2013-03-03
  • C#基礎(chǔ)之泛型

    C#基礎(chǔ)之泛型

    泛型是 2.0 版 C# 語(yǔ)言和公共語(yǔ)言運(yùn)行庫(kù) (CLR) 中的一個(gè)新功能。接下來(lái)通過(guò)本文給大家介紹c#基礎(chǔ)之泛型,感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量

    C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量

    這篇文章主要為大家詳細(xì)介紹了C#如何利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • C#如何遍歷Dictionary

    C#如何遍歷Dictionary

    這篇文章主要為大家詳細(xì)介紹了C#遍歷Dictionary的方法,.NET中的Dictionary是鍵/值對(duì)的集合,使用起來(lái)比較方便,Dictionary也可以用KeyValuePair來(lái)迭代遍歷,感興趣的小伙伴們可以參考一下
    2016-04-04
  • C#通過(guò)NPOI操作Excel的實(shí)例代碼

    C#通過(guò)NPOI操作Excel的實(shí)例代碼

    C#操作Excel的方法有很多種,本文介紹了C#通過(guò)NPOI操作Excel,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01

最新評(píng)論