用vb导出数据到excel文件时,如何生成新的excel文件?
发布网友
发布时间:2022-04-22 22:15
我来回答
共4个回答
热心网友
时间:2022-04-22 18:42
抄一段我写的代码给你,除了能新建一个以外,第二个好处是当记录条数超过65535时,它可以自动新增工作表以将全部数据弄进去。你可以根据自己的情况改改。
Public Conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public xlApp As New Excel.Application
Public xlWb As New Excel.Workbook
Public xlWs As New Excel.Worksheet
Private Sub mnuQueryExcel_Click()
On Error GoTo ErrHandle
Dim wsSeq As Long, i As Long, Length As Long
xlApp.Visible = True
xlApp.ScreenUpdating = False
DataGrid.Visible = False
Length = Rs.Fields.Count - 1
Call Rs.MoveFirst
Set xlWb = xlApp.Workbooks.Add
wsSeq = 0
Do While Not Rs.EOF
wsSeq = wsSeq + 1
Set xlWs = xlWb.Worksheets.Add
xlWs.Name = "查询结果" & CStr(wsSeq)
For i = 0 To Length
xlWs.Cells(1, i + 1).Value = "'" & Rs.Fields(i).Name
Next i
Call xlWs.Cells(2, 1).CopyFromRecordset(Rs)
Loop
Call Rs.MoveFirst
DataGrid.Visible = True
xlApp.ScreenUpdating = True
Call MsgBox("写入Excel完毕。", vbInformation)
Exit Sub
ErrHandle:
Call MsgBox("错误代码:" & Hex(Err.Number) & vbCrLf & "错误描述:" & Err.Description, vbCritical)
End Sub
热心网友
时间:2022-04-22 20:00
在单击事件加入一commondialoge控件,获取输入的文件名,放在变量R中,再把代码中的相应文件名改为R应该可以。
热心网友
时间:2022-04-22 21:35
文件名:text1.text
Open App.Path & "\" & text1.Text & ".xls" For Binary As #11
Close #11
加上这句看看。
热心网友
时间:2022-04-22 23:26
源程序不是很完整么,保存对话框也有,你想怎么生成新文件?