C#使用NPOI將excel導(dǎo)入到list的方法
本文實(shí)例為大家分享了C#使用NPOI將excel導(dǎo)入到list的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)是確定是實(shí)體類接收
/// <summary> /// 將excel導(dǎo)入到list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fs">Stream 文件流</param> /// <param name="list">轉(zhuǎn)換的Dictionary:例如 ("昵稱","nickname")</param> /// <returns></returns> public static List<T> ExcelToList<T>(this Stream fs, Dictionary<string, string> list) where T : class, new() ? ? ? ? { ? ? ? ? ? ? List<T> ts = new List<T>(); ? ? ? ? ? ? IWorkbook workbook = null; ? ? ? ? ? ? ISheet sheet = null; ? ? ? ? ? ? T t = new T(); ? ? ? ? ? ? List<string> listName = new List<string>(); ? ? ? ? ? ? try ? ? ? ? ? ? { ? // 獲得此模型的公共屬性 ? ? ? ? ? ? ? ? var propertys = t.GetType().GetProperties().ToList(); ? ? ? ? ? ? ? ? workbook = new HSSFWorkbook(fs); ? ? ? ? ? ? ? ? if (workbook != null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sheet = workbook.GetSheetAt(0);//讀取第一個(gè)sheet,當(dāng)然也可以循環(huán)讀取每個(gè)sheet ? ? ? ? ? ? ? ? ? ? if (sheet != null) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? int rowCount = sheet.LastRowNum;//總行數(shù) ? ? ? ? ? ? ? ? ? ? ? ? if (rowCount > 0) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? IRow firstRow = sheet.GetRow(0);//第一行 ? ? ? ? ? ? ? ? ? ? ? ? ? ? int cellCount = firstRow.LastCellNum;//列數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? //循環(huán)列數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < cellCount; i++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //循環(huán)需要轉(zhuǎn)換的值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? foreach (var item in list) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (item.Key.Equals(firstRow.GetCell(i).StringCellValue)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //替換表頭 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? firstRow.GetCell(i).SetCellValue(item.Value); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取已經(jīng)替換的表頭 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var s = firstRow.GetCell(i).StringCellValue; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加到listname ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? listName.Add(s); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 1; i <= rowCount; i++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? t = new T(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? IRow currRow = sheet.GetRow(i);//第i行 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int k = 0; k < cellCount; k++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? //取值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? object value = null; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (currRow.GetCell(k) != null) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? firstRow.GetCell(0).SetCellType(CellType.String); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? currRow.GetCell(k).SetCellType(CellType.String); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value = currRow.GetCell(k).StringCellValue; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var Name = string.Empty; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取第表頭的值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Name = listName[k]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //循環(huán)屬性 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? foreach (var pi in propertys) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (pi.Name.Equals(Name)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取屬性類型名稱 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var s = pi.PropertyType.Name; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果非空,則賦給對(duì)象的屬性 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (value != DBNull.Value) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷屬性的類型(可以自行添加) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (s) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Guid": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, new Guid(value.ToString()), null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Int32": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, value.ToString() == "" ? 0 : Convert.ToInt32(value.ToString()), null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Decimal": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, value.ToString() == "" ? 0 : Convert.ToDecimal(value.ToString()), null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "DateTime": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, Convert.ToDateTime(value.ToString()), null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Double": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, value.ToString() == "" ? 0 : Convert.ToDouble(value.ToString()), null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "String": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pi.SetValue(t, value, null); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //對(duì)象添加到泛型集合中 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ts.Add(t); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return ts; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (fs != null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? fs.Close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return null; ? ? ? ? ? ? } ? ? ? ? }
調(diào)用
?var list = new Dictionary<string, string>(); ? ? ? ? ? ? list.Add("ID", "TradeAccountId"); ? ? ? ? ? ? list.Add("簡(jiǎn)介", "Intro"); ? ? ? ? ? ? list.Add("昵稱1", "Nickname"); ? ? ? ? ? ? list.Add("限制人數(shù)", "SubscribeLimit"); ? ? ? ? ? ? list.Add("模式", "SubscribeMode"); ? ? ? ? ? ? list.Add("收益率", "ProfitRate"); ? ? ? ? ? ? list.Add("收益", "ProfitLossAmount"); ? ? ? ? ? ? list.Add("頭像", "Img"); ? ? ? ? ? ? list.Add("平臺(tái)名稱", "Name"); ? ? ? ? ? ? list.Add("用戶昵稱", "UserNickname"); ? ? ? ? ? ? FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\Test\Test\bin\Debug\Export\2021-04-27-14-46-36.xls", FileMode.Open); ? ? ? ? ? ? var list3 = fs.ExcelToList<Res_Signal>(list);
實(shí)體類
[Serializable] public class Res_Signal ? ? { ? ? ? ? /// <summary> ? ? ? ? /// 交易賬號(hào)ID ? ? ? ? /// </summary> ? ? ? ? public Guid TradeAccountId { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 交易賬號(hào)簡(jiǎn)介 ? ? ? ? /// </summary> ? ? ? ? public String Intro { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 交易賬號(hào)昵稱 ? ? ? ? /// </summary> ? ? ? ? public String Nickname { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 訂閱限制值 ? ? ? ? /// </summary> ? ? ? ? public Int32 SubscribeLimit { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 訂閱模式 目前都是免費(fèi) ?0免費(fèi) ?1收費(fèi) ? ? ? ? /// </summary> ? ? ? ? public Int32 SubscribeMode { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 訂閱模式名稱 ? ? ? ? /// </summary> ? ? ? ? public String SubscribeMode_Des { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 平臺(tái)名稱 ? ? ? ? /// </summary> ? ? ? ? public String Name { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 頭像 ? ? ? ? /// </summary> ? ? ? ? public String HeadImg { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 用戶昵稱 ? ? ? ? /// </summary> ? ? ? ? public String UserNickname { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 訂閱人數(shù) ? ? ? ? /// </summary> ? ? ? ? public Int32 SubscribeCount { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 平臺(tái)圖片 ? ? ? ? /// </summary> ? ? ? ? public String Img { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 收益率 ? ? ? ? /// </summary> ? ? ? ? public decimal ProfitRate { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 總收益 ? ? ? ? /// </summary> ? ? ? ? public decimal ProfitLossAmount { get; set; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#利用NPOI操作Excel(單元格設(shè)置)
- C# 基于NPOI操作Excel
- c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet
- C#通過(guò)NPOI導(dǎo)入導(dǎo)出數(shù)據(jù)EXCEL
- C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類
- C#使用Npoi導(dǎo)出Excel并合并行列
- C#使用NPOI讀取excel轉(zhuǎn)為DataSet
- C#使用NPOI庫(kù)讀寫Excel文件
- C#使用NPOI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出功能
- C#使用NPOI設(shè)置Excel下拉選項(xiàng)
相關(guān)文章
利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼
利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼,需要的朋友可以參考一下2013-04-04C#實(shí)現(xiàn)讀取USB轉(zhuǎn)串口參數(shù)并顯示在ComboBox
在很多應(yīng)用程序中,尤其是那些需要與外部硬件通信的程序中,自動(dòng)檢測(cè)和讀取串口參數(shù)是一個(gè)非常有用的功能,下面我們就來(lái)看看如何在C#中實(shí)現(xiàn)這一功能吧2024-01-01Unity3D Shader實(shí)現(xiàn)掃描顯示效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)掃描顯示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03System.Data.OleDb.OleDbException: 未指定的錯(cuò)誤的完美解決方法
本文給大家?guī)?lái)三種有關(guān)System.Data.OleDb.OleDbException: 未指定的錯(cuò)誤的完美解決方法,每種方法都很不錯(cuò),需要的朋友可以參考下2016-09-09