服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - .NET中读取Excel文件的数据及excelReader应用

.NET中读取Excel文件的数据及excelReader应用

2019-10-22 10:34asp.net教程网 ASP.NET教程

轻量,快速的C#编写的库读取Microsoft Excel文件,这对读取大量excel文件的朋友们很有帮助而且可以学习下ExcelDataReader的应用,感兴趣的朋友可以了解下,或许对你有所帮助

项目介绍 
轻量,快速的C#编写的库读取Microsoft Excel文件('97-2007)。 
跨平台: 
- Windows with .Net Framework 2 
- Windows Mobile with Compact Framework 
- Linux, OS X, BSD with Mono 2+ 
如何使用 
C# 代码: 

复制代码代码如下:


FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); 
//1. Reading from a binary Excel file ('97-2003 format; *.xls) 
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); 
//... 
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx) 
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 
//... 
//3. DataSet - The result of each spreadsheet will be created in the result.Tables 
DataSet result = excelReader.AsDataSet(); 
//... 
//4. DataSet - Create column names from first row 
excelReader.IsFirstRowAsColumnNames = true; 
DataSet result = excelReader.AsDataSet(); 
//5. Data Reader methods 
while (excelReader.Read()) 

//excelReader.GetInt32(0); 

//6. Free resources (IExcelDataReader is IDisposable) 
excelReader.Close(); 


VB.NET 代码: 

复制代码代码如下:


Dim stream As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read) 
'1. Reading from a binary Excel file ('97-2003 format; *.xls) 
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream) 
'... 
'2. Reading from a OpenXml Excel file (2007 format; *.xlsx) 
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream) 
'... 
'3. DataSet - The result of each spreadsheet will be created in the result.Tables 
Dim result As DataSet = excelReader.AsDataSet() 
'... 
'4. DataSet - Create column names from first row 
excelReader.IsFirstRowAsColumnNames = True 
Dim result As DataSet = excelReader.AsDataSet() 
'5. Data Reader methods 
While excelReader.Read() 
'excelReader.GetInt32(0); 
End While 
'6. Free resources (IExcelDataReader is IDisposable) 
excelReader.Close() 

延伸 · 阅读

精彩推荐