VB 操作Excel,附软件图
一、 VB读写EXCEL表:
VB本身提自动化功能可以读写EXCEL表,其方法如下:
1、在工程中引用Microsoft Excel类型库:
从"工程"菜单中选择"引用"栏;选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。表示在工程中要引用EXCEL类型库。
2、在通用对象的声明过程中定义EXCEL对象:
Dim xlApp As Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.Worksheet
3、在程序中操作EXCEL表常用命令:
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open("文件名") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("表名") '设置活动工作表
xlSheet.Cells(row, col) =值 '给单元格(row,col)赋值
xlSheet.PrintOut '打印工作表
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL启动宏
xlBook.RunAutoMacros (xlAutoClose) '运行EXCEL关闭宏
4、在运用以上VB命令操作EXCEL表时,除非设置EXCEL对象不可见,否则VB程序可继续执行其它操作,也能够关闭EXCEL,同时也可对EXCEL进行操作。但在EXCEL操作过程中关闭EXCEL对象时,VB程序无法知道,如果此时使用EXCEL对象,则VB程序会产生自动化错误。形成VB程序无法完全控制EXCEL的状况,使得VB与EXCEL脱节。
二、 EXCEL的宏功能:
EXCEL提供一个Visual Basic编辑器,打开Visual Basic编辑器,其中有一工程属性窗口,点击右键菜单的"插入模块",则增加一个"模块1",在此模块中可以运用Visual Basic语言编写函数和过程并称之为宏。其中,EXCEL有两个自动宏:一个是启动宏(Sub Auto_Open()),另一个是关闭宏(Sub Auto_Close())。它们的特性是:当用EXCEL打含有启动宏的工簿时,就会自动运行启动宏,同理,当关闭含有关闭宏的工作簿时就会自动运行关闭宏。但是通过
相关文档:
'读数据到二进制字段
Public Sub ReadfromBLOB(filed As ADODB.Field, Filen As String)
Dim DataFile As Integer, Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, i As Integer
'传送块单位大小
Const ChunkSize As Integer = 16384
Dim MediaTemp As String
Dim lngOffset As Long
Di ......
VB根据窗口标题获取应用程序完整路径(来自网络)
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId A ......
Private Sub Cmd_OK_Click()
Dim ExcelAppX As Excel.Application
Dim ExcelBookX As Excel.Workbook
Dim ExcelSheetX As Excel.Worksheet
Dim a(1 To 3) As Single
Dim strFormat As Variant
......
Welcome to Microsoft Developer Support, Languages team blog! You will find a lot of language related troubleshooting resources here.
Troubleshooting PInvoke Related Issues
I am back with some more PInvoke Stuff. Recently I was working on a PInvoke issue which I found interesting ......
【函数】
GetFileVersionInfo
【操作系统】
Win9X:Yes
WinNT:Yes
【声明】
GetFileVersionInfo Lib "version.dll" Alias "GetFileVersionInfoA" &n ......