VB导出到EXCEL,怎么样使第一行为数据库的列名
解决方案 »
- dll 可以这么做吗?
- 用VB+API如何向我的网站上传一个图像文件aa.jpg
- 如何把在PictureBox里绘制的图片复制到剪贴板??大侠救命>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- 用Winsock控件实现网络通讯问题
- 安装水晶报表9的朋友请进
- EXCEL中用VBA怎么获得批注的信息
- listview问题求助
- VB是不是真的不能随分辨率改变界面啊
- 请教 删除约束名
- 谁遇到过VB6的制作的安装包安装时Setup出错,不能安装的情况?高分赠高手!
- 哪里有允许上传一个90M文件的免费空间?
- vb MDI 子窗体 最大化 无最大化最小化按钮 和无关闭按钮,应怎样做
cell(1,0)=""
cell(1,1)=""......
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim mrcc As ADODB.Recordset
Dim tebox As String
Dim txtsql As String
txtsql = zongsql
Set mrcc = executesql(txtsql, tebox)
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
xlApp.Cells.CopyFromRecordset mrcc
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Microsoft Excel(*.xls) | *.xls"
CommonDialog1.Flags = cdlOFNCreatePrompt + cdlOFNHideReadOnly
CommonDialog1.ShowSave
If Err = cdlCancel Then
xlApp.DisplayAlerts = False
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Exit Sub
Else
xlBook.SaveAs CommonDialog1.FileName
On Error GoTo saveerror_exit
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Set mrcc = Nothing
Exit Sub
End If
saveerror_exit:
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Exit Sub
End Sub以上是导出代码,可以成功导出,但是没有数据库的列名,我不想用循环填充,有没有解决的方法.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim mrcc As ADODB.Recordset
Dim tebox As String
Dim txtsql As String
Dim i As Integer
txtsql = zongsql
Set mrcc = executesql(txtsql, tebox)
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.worksheets(1)
'填上字段名
For i = 0 To rs.fields.Count
xlsheet.cells(1, i + 1) = rs.fields(i).Name
Next
xlsheet.range("a2").CopyFromRecordset mrcc
'xlApp.Cells.CopyFromRecordset mrcc
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Microsoft Excel(*.xls) | *.xls"
CommonDialog1.Flags = cdlOFNCreatePrompt + cdlOFNHideReadOnly
CommonDialog1.ShowSave
If Err = cdlCancel Then
xlApp.DisplayAlerts = False
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Exit Sub
Else
xlBook.SaveAs CommonDialog1.FileName
On Error GoTo saveerror_exit
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Set mrcc = Nothing
Exit Sub
End If
saveerror_exit:
xlBook.Close
xlApp.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
mrcc.Close
Exit Sub
End Sub