VB导出到EXCEL,怎么样使第一行为数据库的列名

解决方案 »

  1.   

    可以使用EXCEL对象直接写!!
    cell(1,0)=""
    cell(1,1)=""......
      

  2.   

    Private Sub daochu_Click()
           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以上是导出代码,可以成功导出,但是没有数据库的列名,我不想用循环填充,有没有解决的方法.
      

  3.   

    写了下,没有VB,就没有测试了,楼主试下,应该可行Private Sub daochu_Click()
                  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