網(wǎng)站首頁 編程語言 正文
NPOI是指構(gòu)建在POI 3.x版本之上的一個程序,NPOI可以在沒有安裝Office的情況下對Word或Excel文檔進行讀寫操作。 NPOI是一個開源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項目。
以下代碼主要分3部分:通過實體類的自定義特性導(dǎo)出Excel文件
1、封裝類:ExcelHelper
2、實體類:StudentModel
3、調(diào)用:Form1
ExcelHelper
?////// Excel操作類 /// ///實體類 public class ExcelHelperwhere T : class ? ? { ? ? ? ? /// ? ? ? ? /// 保存Excel文件 ? ? ? ? /// ? ? ? ? /// Excel文件名 ? ? ? ? /// Sheet工作表名 ? ? ? ? /// 實體類對象 ? ? ? ? public static void SaveExcelFile(string excelName, string sheetName, Listdata) ? ? ? ? { ? ? ? ? ? ? IWorkbook workBook = new HSSFWorkbook(); //創(chuàng)建一個Excel文檔 ? ? ? ? ? ? ISheet sheet = workBook.CreateSheet(sheetName); //創(chuàng)建一個工作表Sheet ? ? ? ? ? ? int rowNum = 0; ? ? ? ? ? ? var row = sheet.CreateRow(sheet.LastRowNum); //LastRowNum記錄當前可用寫入的行索引 ? ? ? ? ? ? PropertyInfo[] preInfo = typeof(T).GetProperties();//獲取這個實體對象的所有屬性 ? ? ? ? ? ? foreach (var item in preInfo) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? object[] objPres = item.GetCustomAttributes(typeof(DescriptionAttribute), true);//獲取當前屬性的自定義特性列表 ? ? ? ? ? ? ? ? if (objPres.Length > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < objPres.Length; i++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? row.CreateCell(rowNum).SetCellValue(((DescriptionAttribute)objPres[i]).Description);//創(chuà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(); ?//獲取當前對象的屬性列表 ? ? ? ? ? ? ? ? foreach (var itemPropSub in itemProps) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //獲取當前對象特性中的自定義特性[Description("自定義特性")] ? ? ? ? ? ? ? ? ? ? var objs = itemPropSub.GetCustomAttributes(typeof(DescriptionAttribute), true); ? ? ? ? ? ? ? ? ? ? if (objs.Length > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //將當前對象的特性值,插入當前行的第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
//實體類 public class StudentModel ? ? { ? ? ? ? [Description("學(xué)號")] ? ? ? ? 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)用:
ListstdList = new List (); 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 .SaveExcelFile(Application.StartupPath + "\\StudentInfo.xls", "Student", stdList);
結(jié)果:
原文鏈接:https://blog.csdn.net/qq_34699535/article/details/106225726
相關(guān)推薦
- 2022-12-05 Golang中的錯誤處理的示例詳解_Golang
- 2022-04-21 IDEA - Spring Boot 項目 application.yml 文件不加載的問題
- 2022-10-10 matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例_python
- 2022-08-16 C#獲取Description特性的擴展類詳解_C#教程
- 2023-02-23 Rust個人學(xué)習(xí)小結(jié)之Rust的循環(huán)_Rust語言
- 2023-01-07 詳解C++11中綁定器bind的原理與使用_C 語言
- 2022-09-29 React路由攔截模式及withRouter示例詳解_React
- 2023-05-09 Oracle中分割字符串的方法實例代碼_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支