如何从Excel文件中导入所有Sheet到DataTable中?

如果需要从外部存储的测试数据文件例如 Excel 中导入 Sheet 页到 DataTable 中,并且判断 DataTable 中是否存在指定的 Sheet ,如果不存在则添加 Sheet 。可以例如 DataTable 对象的 GetSheet AddSheet 以及 ExcelCOM 对象编程来实现:

Function ImportAllSheets(ByVal FileName)

Dim oExcel, oBook

'Launch excel

Set oExcel = GetObject("", "Excel.Application")

'Open the file in read only mode

Set oBook = oExcel.WorkBooks.Open(FileName,,True)

'Enumerate through all the sheets present in the file

For each oSheet in oBook.WorkSheets

'Check if a DataTable with current name already exists

If Not IfDataSheetExist(oSheet.Name) Then

'DataTable cannot be imported if the sheet does not exist

DataTable.AddSheet oSheet.Name

End If

'Import the sheet

DataTable.ImportSheet FileName, oSheet.Name,oSheet.Name

Next

Set oBook = Nothing

'Quit Excel

oExcel.Quit

Set oExcel = Nothing

End Function

Function IfDataSheetExist(ByVal SheetName)

IfDataSheetExist = True

On error resume next

Dim oTest

Set oTest = DataTable.GetSheet(SheetName)

If err.number Then IfDataSheetExist = False

On error goto 0

End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值