C#使用NPOI将excel导入到list的方法
作者:ゞ瀟灑亾眚ゞ 时间:2023-11-17 22:49:09
本文实例为大家分享了C#使用NPOI将excel导入到list的具体代码,供大家参考,具体内容如下
这个是确定是实体类接收
/// <summary>
/// 将excel导入到list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fs">Stream 文件流</param>
/// <param name="list">转换的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);//读取第一个sheet,当然也可以循环读取每个sheet
if (sheet != null)
{
int rowCount = sheet.LastRowNum;//总行数
if (rowCount > 0)
{
IRow firstRow = sheet.GetRow(0);//第一行
int cellCount = firstRow.LastCellNum;//列数
//循环列数
for (int i = 0; i < cellCount; i++)
{
//循环需要转换的值
foreach (var item in list)
{
if (item.Key.Equals(firstRow.GetCell(i).StringCellValue))
{
//替换表头
firstRow.GetCell(i).SetCellValue(item.Value);
}
}
//获取已经替换的表头
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];
//循环属性
foreach (var pi in propertys)
{
if (pi.Name.Equals(Name))
{
//获取属性类型名称
var s = pi.PropertyType.Name;
//如果非空,则赋给对象的属性
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;
}
}
}
}
}
//对象添加到泛型集合中
ts.Add(t);
}
}
}
}
return ts;
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
return null;
}
}
调用
var list = new Dictionary<string, string>();
list.Add("ID", "TradeAccountId");
list.Add("简介", "Intro");
list.Add("昵称1", "Nickname");
list.Add("限制人数", "SubscribeLimit");
list.Add("模式", "SubscribeMode");
list.Add("收益率", "ProfitRate");
list.Add("收益", "ProfitLossAmount");
list.Add("头像", "Img");
list.Add("平台名称", "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);
实体类
[Serializable]
public class Res_Signal
{
/// <summary>
/// 交易账号ID
/// </summary>
public Guid TradeAccountId { get; set; }
/// <summary>
/// 交易账号简介
/// </summary>
public String Intro { get; set; }
/// <summary>
/// 交易账号昵称
/// </summary>
public String Nickname { get; set; }
/// <summary>
/// 订阅限制值
/// </summary>
public Int32 SubscribeLimit { get; set; }
/// <summary>
/// 订阅模式 目前都是免费 0免费 1收费
/// </summary>
public Int32 SubscribeMode { get; set; }
/// <summary>
/// 订阅模式名称
/// </summary>
public String SubscribeMode_Des { get; set; }
/// <summary>
/// 平台名称
/// </summary>
public String Name { get; set; }
/// <summary>
/// 头像
/// </summary>
public String HeadImg { get; set; }
/// <summary>
/// 用户昵称
/// </summary>
public String UserNickname { get; set; }
/// <summary>
/// 订阅人数
/// </summary>
public Int32 SubscribeCount { get; set; }
/// <summary>
/// 平台图片
/// </summary>
public String Img { get; set; }
/// <summary>
/// 收益率
/// </summary>
public decimal ProfitRate { get; set; }
/// <summary>
/// 总收益
/// </summary>
public decimal ProfitLossAmount { get; set; }
}
来源:https://blog.csdn.net/qq_41078088/article/details/116239479
标签:C#,NPOI,excel
0
投稿
猜你喜欢
五分钟手撸一个Spring容器(萌芽版)
2021-07-29 02:41:54
一文详解如何在控制台显示MyBatis的SQL语句
2023-01-09 06:43:38
java8 如何实现分组计算数量和计算总数
2022-05-05 01:17:32
Java中实现多线程关键词整理(总结)
2023-09-08 16:08:49
JAVA图片水印开发案例详解
2023-06-20 02:06:53
详解eclipse下创建第一个spring boot项目
2022-01-02 10:36:13
Android Fragment的回退栈示例详细介绍
2023-06-21 12:30:55
Android开发之文件操作详解
2023-02-06 01:23:30
实例讲解Java并发编程之闭锁
2023-10-25 14:25:07
Android DatePicker和DatePickerDialog基本用法示例
2022-08-24 13:33:10
C#利用子线程刷新主线程分享教程
2023-05-16 13:25:46
Android模拟实现网易新闻客户端
2022-09-03 18:19:11
聊一聊SpringBoot服务监控机制
2023-02-09 02:47:48
Unity实现VR中在黑板上写字效果
2021-08-04 20:11:11
迅速掌握Java容器中常用的ArrayList类与Vector类用法
2022-12-16 16:18:47
C# 嵌入dll 的方法
2022-11-06 08:39:10
Spring interceptor拦截器配置及用法解析
2023-06-26 06:08:15
Android 钱包支付之输入支付密码的实现步骤
2021-09-23 11:17:09
将应用程序进行Spring6迁移的最佳使用方式
2021-08-28 12:03:58
java开发微信公众号支付
2021-10-24 16:02:40