导入Excel(xlsx)到List

/// <summary>
/// 导入Excel(xlsx)到List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream"></param>
/// <param name="sheetIndex">从零开始sheet索引</param>
/// <param name="headerRowCount">表头行数,即从此开始读取数据</param>
/// <returns></returns>
public static List<T> ExcelToList<T>(MemoryStream stream,int sheetIndex, int headerRowCount)
{
List<T> list = new List<T>();
IWorkbook workbook = new XSSFWorkbook();
try
{
workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(sheetIndex);
IRow cellNum = sheet.GetRow(0);
var propertys = typeof(T).GetProperties();
string cellValue = null;
int lastCellNum = cellNum.LastCellNum;

for (int i = headerRowCount; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
var tInstance = System.Activator.CreateInstance<T>();
for (int j = 0; j < lastCellNum; j++)
{
cellValue = row.GetCell(j) != null ? row.GetCell(j).ToString() : null;

string propertyTypeFullName = (propertys[j].PropertyType).FullName;

if (IsNullableType(propertys[j].PropertyType))
{
if (string.IsNullOrWhiteSpace(cellValue))
{
propertys[j].SetValue(tInstance, null, null);
continue;
}
propertyTypeFullName = Nullable.GetUnderlyingType(propertys[j].PropertyType).FullName;
}

switch (propertyTypeFullName)
{
case "System.String":
propertys[j].SetValue(tInstance, cellValue, null);
break;
case "System.DateTime":
DateTime dtParam = Convert.ToDateTime(cellValue, CultureInfo.InvariantCulture);
propertys[j].SetValue(tInstance, dtParam, null);
break;
case "System.Boolean":
bool blParam = Convert.ToBoolean(cellValue);
propertys[j].SetValue(tInstance, blParam, null);
break;
case "System.Int16":
short int16Param = Convert.ToInt16(cellValue);
propertys[j].SetValue(tInstance, int16Param, null);
break;
case "System.Int32":
int int32Param = Convert.ToInt32(cellValue);
propertys[j].SetValue(tInstance, int32Param, null);
break;
case "System.Int64":
long int64Param = Convert.ToInt64(cellValue);
propertys[j].SetValue(tInstance, int64Param, null);
break;
case "System.Byte":
byte btPrame = Convert.ToByte(cellValue);
propertys[j].SetValue(tInstance, btPrame, null);
break;
case "System.Single":
float sgParam = Convert.ToSingle(cellValue);
propertys[j].SetValue(tInstance, sgParam, null);
break;
case "System.Double":
double dbParam = Convert.ToDouble(cellValue);
propertys[j].SetValue(tInstance, dbParam, null);
break;
default:
propertys[j].SetValue(tInstance, null, null);
break;
}
}

list.Add(tInstance);
}
stream.Flush();
stream.Close();
workbook.Close();
}
finally
{
stream.Close();
workbook.Close();
}
return list;
}

原文地址:https://www.cnblogs.com/starts/p/11155891.html

时间: 2024-08-01 05:46:05

导入Excel(xlsx)到List的相关文章

关于导入excel报错的处理(xls,xlsx)

最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strConnection = "Provider=Microsoft.JET.OLEDB.4.0;Data Source = " + strFileName + ";Extended Properties = Excel 8.0"; 这样写连接串导入*xls时一切顺利,当我导入*xlsx文件时报这样一个错误:“System.Da

ASP.NET Core 导入导出Excel xlsx 文件

ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Linux和Mac. EPPlus.Core 是基于EPPlus 更改而来,在Linux 下需要安装libgdiplus . EPPlus:http://epplus.codeplex.com/ EPPlus.Core:https://github.com/VahidN/EPPlus.Core 下面在A

Thinkphp 用PHPExcel 导入Excel

搞了个简单的Excel导入, 用的是PHPExcel(百科:用来操作Office Excel文档的一个PHP类库, 基于微软的OpenXML标准和PHP语言) 好, 不说了, 开始吧... 首先得有PHPExcel类库, 点这里下载 https://github.com/Zmwherein/PHPExcel.git 然后把它放在 \ThinkPHP\Library\Vendor(个人喜好, 能引入就行了) 如图: PHPExcel.php 类似一个入口文件. 可以进去看看里面写的方法是怎个跑法.

使用jxls技术导入Excel模版数据(转自其他博客)

第一步:先确定好Excel导入的格式以及各表格字段值的含义 第二步:定义好解析的XML--videoConfig.xml <?xml version="1.0" encoding="UTF-8"?> <workbook> <worksheet name="Sheet1"> <section startRow="0" endRow="0"/> <loop

JSP导入EXCEL样式

http://demo.gcpowertools.com.cn/spreadjs/exceliosample/exceliosample/ Java实现导入Excel: 1.做一个jsp页面,页面包括浏览文件,提交文件 2.将excel文件上传到服务器 3.  服务器对该excel文件进行读出 4.  将excel文件内容显示到页面上 环境搭建: 需要准备的包:commons-fileupload-1.2.1.jar & commons-io-1.3.2.jar 这两个包是上传用的 jxl.ja

C# 导入EXCEL 报错外部表不是预期的格式错误 .

错误经过:在读取Excel时,出现外部表不是预期的格式 错误原因1: 由于Excel 97-2003的连接格式与Excel 2010 的 不同造成. 以下是从网上摘抄原文 Excel “External table is not in the expected format.” . Question: I'm trying to read an Excel (xlsx) file using the code shown below. I get an "External table is no

一步步实现ABAP后台导入EXCEL到数据库【1】

在SAP的应用当中,导入.导出EXCEL文件的情况是一个常见的需求,有时候用户可能有大量的数据需要定期导入到SAP的数据库中.这种情况下,使用导入程序在前台导入可能要花费不少的时间,如果能安排导入程序为后台作业,既可以节约用户的时间,也可以有效利用闲时的服务器资源.下面来介绍一下相关的知识和具体实践办法. 本文链接:http://www.cnblogs.com/hhelibeb/p/5912330.html 原创内容,转载请注明 1,定义结构 首先,准备一个EXCEL文件.假设一个相对简单的情景

Asp.NET MVC 导入Excel数据教程 手把手教你系列!!!

先上效果图 1.引言 小弟最近接了个大学生的毕业设计,收了100块钱让做一个ASP.NET MVC导入Excel到数据库的功能,由于以前没做过就到处搜索资料,但是发现网上的资料不是直接贴一大堆乱起八遭的源码,就是需要借用NPOI第三方类库太麻烦了,况且预算才100RMB简直不值得,所以小弟尝试自己动手丰衣足食,所以就有了这篇博客. 先上一张小弟的思路图: (说明:没有安装做流程图的软件!凑合着看吧) 2 进入正题 首先跟着小弟先创建一个默认的MVC项目(相信大家都会创建,这里就不演示了) 第一步

ThinkPHP导入Excel文件(使用PHPExcel)

主要知识点,用PHPExcel导入Excel数据经过这几天测试还是可以,xls,xlsx都可以获取Excel的数据.下载地址:http://phpexcel.codeplex.com/ O.开发思路 1.先把Excel文件上传到服务器 2.获取服务器Excel文件内容 3.写入数据库 一.上传Excel文件,使用PHP里自带的上传方法 “\Think\Upload();”,可以很方便的实现.为此我整理下使用这个方法的最简单方式 /** * TODO 上传文件方法 * @param $fileid