65.9K
CodeProject 正在变化。 阅读更多。
Home

读取数据的 Excel 文件类

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.23/5 (11投票s)

2007年9月12日

CPOL
viewsIcon

46553

downloadIcon

431

用于读取数据的 Excel 文件类。

Screenshot - ExcelFile.jpg

引言

这是一个我编写的用于处理 Excel 文件的实用小类。我在 Google 上搜索了一下,可以找到一些读取数据的片段,还有一些读取工作表名称的片段,但我找不到一个可重用的类,让我能够同时完成这两项任务。所以我自己写了一个。

背景

也许它并不令人兴奋,但它是可重用的,而且我真的很喜欢面向对象编程,只要有机会使用它。对于我需要完成的任务,我只需要读取和浏览 Excel 文件,但如果其他人发现这个类并扩展它,以便将所有工作表加载到一个数据集中,操作数据,并最终将数据保存回文件,那就太棒了。我实际上需要回到实际工作了,否则我会很遗憾的 :(

使用代码

'http://www.DTS-Global.com
'Author: Allen Crane Sr. Developer
'instanciate the new handy dandy new object I wrote :) 
Dim Excel As New ExcelFile(paths, strFileNameOnly)

'instanciate the new handy dandy new object I wrote :) 
Response.Write("Your workbook contains " worksheets" & _
               Excel.ExcelWorkSheets.Count & ")

'instanciate the new handy dandy new object I wrote :) 
Dim Excel As New ExcelFile(paths, strFileNameOnly)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'load all the work sheet names in a radio button list
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'counter for loop
Dim i As Int16 = Nothing
'loop through all the transactions and add all the items
For i = 0 To Excel.ExcelWorkSheets.Count - 1
  rblMultipleSheets.Items.Add(Excel.ExcelWorkSheets.Item(i).TableName)
Next

'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Bind to the data grid with the first work sheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''
DataGrid1.DataSource = _
    Excel.GetWorkSheetByName(Excel.ExcelWorkSheets.Item(0).TableName)
DataGrid1.DataBind()

历史

  • 版本 1.0
© . All rights reserved.