欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 12:46:30
项目介绍 轻量,快速的c#编写的库读取microsoft excel文件('97-2007)。 跨平台: - windows with .net framework 2 -...
项目介绍
轻量,快速的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()