现在DBGrid上已经显示了想显示的东西,当一点击某一按钮是把DBGrid的数据用excel显示?
请各位帮忙!

解决方案 »

  1.   

    引用Microsoft Excel类型库:从"工程"菜单中选择"引用"栏;选择Microsoft Excel 8.0 Object Library;选择"确定"。在FORM的LOAD事件中加入:
      Data1.DatabaseName = 数据库名称
      Data1.RecordSource = 表名
      Data1.Refresh在按钮的CLICK事件中加入
      Dim Irow, Icol As Integer
      Dim Irowcount, Icolcount As Integer
      Dim Fieldlen() "存字段长度值
      Dim xlApp As Excel.Application
      Dim xlBook As Excel.Workbook
      Dim xlSheet As Excel.Worksheet  Set xlApp = CreateObject("Excel.Application")
      Set xlBook = xlApp.Workbooks.Add
      Set xlSheet = xlBook.Worksheets(1)  With Data1.Recordset
      .MoveLast  If .RecordCount < 1 Then
        MsgBox ("Error 没有记录!")
        Exit Sub
      End If  Irowcount = .RecordCount "记录总数
      Icolcount = .Fields.Count "字段总数  ReDim Fieldlen(Icolcount)
      .MoveFirst   For Irow = 1 To Irowcount + 1
       For Icol = 1 To Icolcount
      Select Case Irow
      Case 1 "在Excel中的第一行加标题
      xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
      Case 2 "将数组FIELDLEN()存为第一条记录的字段长  If IsNull(.Fields(Icol - 1)) = True Then
        Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
         "如果字段值为NULL,则将数组Filelen(Icol)的值设为标题名的宽度
      Else
        Fieldlen(Icol) = LenB(.Fields(Icol - 1))
      End If  xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
       "Excel列宽等于字段长
      xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
       "向Excel的CellS中写入字段值
      Case Else
      Fieldlen1 = LenB(.Fields(Icol - 1))  If Fieldlen(Icol) < Fieldlen1 Then
      xlSheet.Columns(Icol).ColumnWidth = Fieldlen1
       "表格列宽等于较长字段长
      Fieldlen(Icol) = Fieldlen1
       "数组Fieldlen(Icol)中存放最大字段长度值
      Else
       xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
      End If  xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
      End Select
      Next
      If Irow <> 1 Then
      If Not .EOF Then .MoveNext
      End If
      Next
      With xlSheet
      .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体"
       "设标题为黑体字
      .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
       "标题字体加粗
      .Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
       "设表格边框样式
      End With
      xlApp.Visible = True "显示表格
      xlBook.Save "保存
      Set xlApp = Nothing "交还控制给Excel
      End With
      

  2.   

    http://www.csdn.net/develop/read_article.asp?id=14952
      

  3.   

    生成一個EXCEL類
    對RECORDSET循環,一格一格的寫
      

  4.   

    你可以直接将记录集插入excel并显示。如:
    Dim CN As ADODB.Connection
    Dim AtRs As ADODB.Recordset
    Dim StrCn As String
    Private Sub Form_Load()
    Set CN = New ADODB.Connection
    StrCn = "driver={SQL server};server=wfxq;uid=sa;pwd=;database=wfrz"
    CN.Open StrCn
    Set AtRs = New ADODB.Recordset
    Set AtRs.ActiveConnection = CN
    AtRs.CursorType = adOpenKeyset
    AtRs.LockType = adLockBatchOptimistic
    End Sub
    Private Sub Command1_Click()
       LoadQuerySQL = "select * from cell"
       If AtRs.State = adStateOpen Then AtRs.Close
       AtRs.Open LoadQuerySQL, StrCn, , , adCmdText
       Set DataGrid1.DataSource = AtRs: DataGrid1.Refresh
    End Sub
    Private Sub Command2_Click()
        If AtRs.RecordCount = 0 Or AtRs.State = adStateClosed Then Exit Sub   Dim xlapp As Excel.Application
       Dim xlbook As Excel.Workbook
       Dim xlsheet As Excel.Worksheet
       Set xlapp = New Excel.Application
       xlapp.Workbooks.Open (App.Path & "\excel.xls")
       Set xlbook = xlapp.Workbooks(1)
       Set xlsheet = xlbook.Worksheets("sheet1") '----------------------------------------&Ccedil;&aring;&iquest;&Otilde;EXECL±í+    For h = 1 To 20
         For l = 1 To 11
          xlsheet.Cells(4 + h, 2 + l).Value = ""
         Next l
        Next h
       xlbook.Save
     '----------------------------------------&Ccedil;&aring;&iquest;&Otilde;EXECL±í-
     '----------------------------------------&cedil;&sup3;&Ouml;&micro;EXECL±í+
       For i = 1 To AtRs.RecordCount
          xlsheet.Cells(i, 1).Value = Trim(AtRs!f1)
          xlsheet.Cells(i, 2).Value = Trim(AtRs!f2)
       Next h
     '----------------------------------------&cedil;&sup3;&Ouml;&micro;EXECL±í-   xlbook.Save
       xlapp.Visible = True
       xlbook.PrintPreview
       xlbook.Close
       xlapp.QuitEnd Sub