网上找的一个通用程序
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(App.Path & "\文件名.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = False '设置EXCEL对象不可见
ShtName'表名
Set xlSheet = xlBook.Worksheets(ShtName) '设置活动工作表发此帖的目的
  请各位大侠帮助补充一下,建立一个通用的调用VB6与Excel的通用模块.

解决方案 »

  1.   

    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关闭宏 
    -----------------------------
      

  2.   

    在VB中,建立一个FORM,在其上放置两个命令按钮,将Command1的Caption属性改为EXCEL,Command2的Caption属性改为End。然后
    在其中输入如下程序:Dim xlApp As Excel.Application '定义EXCEL类 
    Dim xlBook As Excel.Workbook '定义工件簿类
    Dim xlsheet As Excel.Worksheet '定义工作表类 
    Private Sub Command1_Click() '打开EXCEL过程
     If Dir("D:\temp\excel.bz") = "" Then '判断EXCEL是否打开
      Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
      xlApp.Visible = True '设置EXCEL可见
      Set xlBook = xlApp.Workbooks.Open("D:\temp\bb.xls") '打开EXCEL工作簿
      Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
      xlsheet.Activate '激活工作表
      xlsheet.Cells(1, 1) = "abc" '给单元格1行驶列赋值
      xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL中的启动宏
     Else
      MsgBox ("EXCEL已打开") 
     End If
    End SubPrivate Sub Command2_Click()
     If Dir("D:\temp\excel.bz") <> "" Then '由VB关闭EXCEL 
      xlBook.RunAutoMacros (xlAutoClose) '执行EXCEL关闭宏
      xlBook.Close (True) '关闭EXCEL工作簿 
      xlApp.Quit '关闭EXCEL
     End If
     Set xlApp = Nothing '释放EXCEL对象
     End
    End Sub
      2、在D盘根目录上建立一个名为Temp的子目录,在Temp目录下建立一个名为"bb.xls"的EXCEL文件。  3、在"bb.xls"中打开Visual Basic编辑器,在工程窗口中点鼠标键选择插入模块,在模块中输入入下程序存盘:
    Sub auto_open()
     Open "d:\temp\excel.bz" For Output As #1 '写标志文件
     Close #1
    End Sub
    Sub auto_close()
     Kill "d:\temp\excel.bz" '删除标志文件
    End Sub 
      

  3.   

    http://msdn.microsoft.com/zh-cn/library/e9waz863(VS.80).aspx
    示例
    --------------------------------------------------------------------------------下面的示例使用 GetObject 函数来确定 Excel 是否处于运行状态。 VBC#C++F#JScript
    复制' Test to see if a copy of Excel is already running.
    Private Sub testExcelRunning()
        On Error Resume Next
        ' GetObject called without the first argument returns a
        ' reference to an instance of the application. If the
        ' application is not already running, an error occurs.
        Dim excelObj As Object = GetObject(, "Excel.Application")
        If Err.Number = 0 Then
            MsgBox("Excel is running")
        Else
            MsgBox("Excel is not running")
        End If
        Err.Clear()
        excelObj = Nothing
    End Sub下面的示例使用 GetObject 函数获取对特定 Microsoft Excel 工作表 (excelObj) 的引用。调用 GetObject 将返回一个对工作表的引用,该工作表由特定文件 test.xls 表示。接下来的示例代码使 Excel 和包含指定工作表的窗口都可见。此示例需要 Option Strict Off,因为它使用后期绑定,其中对象被赋给 Object 类型的变量。如果您向项目中添加了对 Excel 类型库的引用,则可以指定 Option Strict On 并声明特定对象类型的对象。若要添加该引用,请在 Visual Studio 的“项目”菜单中打开“添加引用”对话框,然后在“COM”选项卡中选择 Excel 类型库。VBC#C++F#JScript
    复制' Add Option Strict Off to the top of your program.
    Option Strict OffVBC#C++F#JScript
    复制Private Sub getExcel()
        Dim fileName As String = "c:\vb\test.xls"    If Not My.Computer.FileSystem.FileExists(fileName) Then
            MsgBox(fileName & " does not exist")
            Exit Sub
        End If    ' Set the object variable to refer to the file you want to use.
        Dim excelObj As Object = GetObject(fileName)
        ' Show Excel through its Application property. 
        excelObj.Application.Visible = True
        ' Show the window containing the file.
        Dim winCount As Integer = excelObj.Parent.Windows.Count()
        excelObj.Parent.Windows(winCount).Visible = True    ' Insert additional code to manipulate the test.xls file here.
        ' ...    excelObj = Nothing
    End Sub