我正在寻找有关这个具体问题的建议:
在Visual Basic 6(VB6)中生成Excel文件(常规XLS,而不是XLSX)的最快方法是什么?
非常感谢。
答案 0 :(得分:3)
最简单的方法是将项目中的引用设置为Excel COM对象,并以编程方式将所有数据插入到工作表中。
答案 1 :(得分: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兼容,但这是我所知道的最简单方法。
答案 2 :(得分: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
答案 3 :(得分:1)
创建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
扩展属性。