如何在VB中,调用Excel.Application对象

如题所述

在工程中引用Microsoft Excel类型库:
从"工程"菜单中选择"引用"栏;选择Microsoft Excel 11.0 Object Library(EXCEL2003),然后选择"确定"。表示在工程中要引用EXCEL类型库。
Dim xlApp As Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.Worksheet
Dim FileName, SheetName As String
FileName = "e:\data.xls" 'excel文件名
SheetName = "sheet1" '表名
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(FileName) '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets(SheetName) '设置活动工作表
'例:
xlSheet.Cells(5, 2) = Text1.Text '给单元格B5赋值
Text2.Text =xlSheet.Cells(2, 3) ‘取C2的值

xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
温馨提示:答案为网友推荐,仅供参考
相似回答