最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

ExcelHelper

?/// <summary>
/// Excel操作類(lèi)
/// </summary>
/// <typeparam name="T">實(shí)體類(lèi)</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í)體類(lèi)對(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)前可用寫(xiě)入的行索引
? ? ? ? ? ? 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)前自定義特性寫(xiě)入
? ? ? ? ? ? ? ? ? ? ? ? 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++;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? //文件流寫(xiě)入
? ? ? ? ? ? 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í)體類(lèi)
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("電話(huà)")]
? ? ? ? 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#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲

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

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 基于params,ref,out的參數(shù)問(wèn)題詳解

    基于params,ref,out的參數(shù)問(wèn)題詳解

    本篇文章是對(duì)params,ref,out的參數(shù)問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C#?WPF實(shí)現(xiàn)顯示本機(jī)網(wǎng)絡(luò)通訊狀態(tài)

    C#?WPF實(shí)現(xiàn)顯示本機(jī)網(wǎng)絡(luò)通訊狀態(tài)

    這篇文章主要為大家詳細(xì)介紹了如何在?WPF?中實(shí)現(xiàn)一個(gè)界面來(lái)顯示本機(jī)網(wǎng)絡(luò)接口的狀態(tài),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2024-12-12
  • C#多線程ThreadPool線程池詳解

    C#多線程ThreadPool線程池詳解

    這篇文章主要為大家詳細(xì)介紹了C#多線程ThreadPool線程池的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)

    BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)

    這篇文章主要介紹了BootStrap mvcpager分頁(yè)樣式(get請(qǐng)求,刷新頁(yè)面)的相關(guān)資料,通過(guò)引入相關(guān)文件,實(shí)現(xiàn)此功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • C#異步編程由淺入深(一)

    C#異步編程由淺入深(一)

    這篇文章主要介紹了C#異步編程由淺入深(一),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 再議C#中的裝箱與拆箱的問(wèn)題詳解

    再議C#中的裝箱與拆箱的問(wèn)題詳解

    本篇文章再次介紹了C#中的裝箱與拆箱,這次們看下使用泛型和不使用泛型引發(fā)裝箱拆箱的情況
    2013-05-05
  • C#雙緩沖技術(shù)實(shí)例詳解

    C#雙緩沖技術(shù)實(shí)例詳解

    這篇文章主要介紹了C#雙緩沖技術(shù),結(jié)合實(shí)例形式較為詳細(xì)的分析了C#雙緩沖的功能,實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-02-02
  • 深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式

    深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式

    這篇文章主要介紹了C#設(shè)計(jì)模式之訂閱發(fā)布模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#結(jié)合JavaScript對(duì)Web控件進(jìn)行數(shù)據(jù)輸入驗(yàn)證的實(shí)現(xiàn)方法

    C#結(jié)合JavaScript對(duì)Web控件進(jìn)行數(shù)據(jù)輸入驗(yàn)證的實(shí)現(xiàn)方法

    在 Web 應(yīng)用的錄入界面,數(shù)據(jù)驗(yàn)證是一項(xiàng)重要的實(shí)現(xiàn)功能,數(shù)據(jù)驗(yàn)證是指確認(rèn) Web 控件輸入或選擇的數(shù)據(jù),本文我們將介紹如何通過(guò)C# 后端及JavaScript 前端對(duì) Web 控件進(jìn)行數(shù)據(jù)輸入有效性的驗(yàn)證,感興趣的朋友可以參考一下
    2024-05-05

最新評(píng)論

商都县| 体育| 从化市| 万载县| 平塘县| 哈巴河县| 彭山县| 云林县| 开原市| 九台市| 大名县| 闻喜县| 蕉岭县| 集贤县| 鄂尔多斯市| 江城| 绥德县| 巴青县| 衡阳市| 巍山| 武冈市| 谢通门县| 安宁市| 巴楚县| 普兰店市| 景泰县| 久治县| 肇东市| 泗水县| 城固县| 新巴尔虎右旗| 丘北县| 云安县| 屏东县| 澄江县| 泰和县| 上饶县| 旬阳县| 玛纳斯县| 新乐市| 梅州市|