【问题标题】:Generating Excel Files with VB6用VB6生成Excel文件
【发布时间】:2009-08-01 17:06:13
【问题描述】:

我正在寻找关于这个特定问题的建议:

在 Visual Basic 6 (VB6) 中生成 Excel 文件(常规 XLS,而不是 XLSX 文件)的最快方法是什么?

非常感谢。

【问题讨论】:

  • 最快=?执行速度还是编码时间/工作量?

标签: excel vb6


【解决方案1】:

最简单的方法是在项目中设置对 Excel COM 对象的引用,并以编程方式将所有数据插入工作表中。

【讨论】:

    【解决方案2】:

    设置对 Excel 对象库的引用(在 VBA 的工具菜单上,在 VB6 中的项目上)(不记得确切的名称,但它会以“Microsoft”开头并在名称中的某处有“Excel” )。

    然后是这样的:

    Public Sub BuildAndSaveWorkbook
    
        With New Excel.Workbook
            ' do all the stuff to create the content, then'
            .SaveAs Filename:="WhateverYouWantToCallIt.xls", FileFormat:=xlExcel8
        End With
    
    End Sub
    

    【讨论】:

    • 啊。忘记了引用存在于 VB6 的另一个菜单中,这些天我只在一台(孤独的、旧的)机器上安装了它。
    【解决方案3】:

    自 Excel 2000 以来,Excel 就能够读取 HTML。

    最简单的方法是编写 HTML 表格并使用 .xls 扩展名保存它们,或者如果它是 Web 应用清除响应缓冲区,请将响应类型设置为“application/vnd.ms-excel” ",然后一无所有地写出表格。

    将以下内容复制并粘贴到记事本中,并使用 .xls 扩展名保存并打开它。

    <table>
    <tr><th>Color</th><th>Shape</th></tr>
    <tr><td>Blue</td><td>Square</td></tr>
    </table>
    

    免责声明:

    我不推荐这种方法,因为它可能只与 Excel 兼容,但它是我所知道的最简单的方法。

    【讨论】:

    • 虽然它确实生成了一个 excel 电子表格,但它仍然是一个用 excel 打开的 html 表格
    • @Ori - 这是正确的,但是如果保存的 excel 足以将其保存为原生格式。我并不是说这是最好的方法,但在 Excel 不可用的情况下,它可以很好地工作。
    【解决方案4】:

    创建 XLS 文件的最快方法是使用 Jet 的 Excel 的 ISAM 驱动程序。下面是如何使用 ADO 和 ADOX 的示例:

    ' References:
    '   Microsoft ActiveX Data Objects 2.8 Library
    '   Microsoft ADO Ext. 2.8 for DDL and Security
    Option Explicit
    
    Private Sub Command1_Click()
        Dim rs              As ADODB.Recordset
    
        Set rs = CreateRecordset( _
            "ID", adDouble, _
            "Name", adVarWChar, 200, _
            "Value", adDouble, _
            "Memo", adLongVarWChar)
        rs.AddNew Array("ID", "Name", "Value", "Memo"), _
            Array(1, "test", 5.1, "long long text here")
        rs.AddNew Array("ID", "Name", "Value"), _
            Array(1, "proba", 15.678)
        AppendExcelSheet rs, App.Path & "\test.xls", "My Data", True
        AppendExcelSheet rs, App.Path & "\test.xls", "More Data"
    End Sub
    
    Private Function CreateRecordset(ParamArray FldDesc()) As ADODB.Recordset
        Dim lIdx            As Long
    
        Set CreateRecordset = New ADODB.Recordset
        With CreateRecordset.Fields
            Do While lIdx < UBound(FldDesc)
                Select Case FldDesc(lIdx + 1)
                Case adDouble, adDate, adCurrency, adBoolean
                    .Append FldDesc(lIdx), FldDesc(lIdx + 1), , adFldIsNullable
                    lIdx = lIdx + 2
                Case adVarWChar
                    .Append FldDesc(lIdx), FldDesc(lIdx + 1), FldDesc(lIdx + 2), adFldIsNullable
                    lIdx = lIdx + 3
                Case adLongVarWChar
                    .Append FldDesc(lIdx), FldDesc(lIdx + 1), -1, adFldIsNullable
                    lIdx = lIdx + 2
                Case Else
                    Err.Raise vbObjectError, , "Not support Excel data type!"
                End Select
            Loop
        End With
        CreateRecordset.Open
    End Function
    
    Private Function AppendExcelSheet( _
                rsSrc As Recordset, _
                sXlsFile As String, _
                Optional ByVal sSheetName As String, _
                Optional ByVal bCreateNew As Boolean) As Boolean
        Dim sConnStr        As String
        Dim oTbl            As ADOX.Table
        Dim oCol            As ADOX.Column
        Dim oFld            As ADODB.Field
        Dim rsDst           As ADODB.Recordset
    
        '--- init local vars
        sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & sXlsFile & ";Extended Properties=""Excel 8.0;Read Only=0"""
        If LenB(sSheetName) = 0 Then
            sSheetName = "Sheet1"
        End If
        '--- cleanup previous file
        If bCreateNew Then
            On Error Resume Next
            SetAttr sXlsFile, vbArchive
            Kill sXlsFile
            On Error GoTo 0
        End If
        '--- create/open workbook and append worksheet
        With New ADOX.Catalog
            .ActiveConnection = sConnStr
            Set oTbl = New ADOX.Table
            oTbl.Name = sSheetName
            For Each oFld In rsSrc.Fields
                Set oCol = New ADOX.Column
                With oCol
                    .Name = oFld.Name
                    .Type = oFld.Type
                End With
                oTbl.Columns.Append oCol
            Next
            .Tables.Append oTbl
        End With
        '--- copy data to range (named after worksheet)
        If rsSrc.RecordCount > 0 Then
            Set rsDst = New ADODB.Recordset
            rsDst.Open "[" & sSheetName & "]", sConnStr, adOpenDynamic, adLockOptimistic
            rsSrc.MoveFirst
            Do While Not rsSrc.EOF
                rsDst.AddNew
                For Each oFld In rsSrc.Fields
                    rsDst.Fields(oFld.Name).Value = oFld.Value
                Next
                rsDst.Update
                rsSrc.MoveNext
            Loop
        End If
    End Function
    

    注意连接字符串上的Read Only=0 扩展属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多