我想吧我查询到得在datagrid中的结果保存为EXCEL文件,该怎么整.下面是我的查询代码.
Dim i As Integer
Dim b As Double
Call OpenCn(a, b, c) '连接数据库If Combo1.Text = Empty Then
Call openRs("select 奶户,牛号,早奶量,晚奶量,挤奶日期,合计 from " & Form6.Combo1.Text & " where 挤奶日期 between '" _
& DTPicker1.Value & "' and '" & DTPicker2.Value & "'") 
Else
Call openRs("select 奶户,牛号,早奶量,晚奶量,挤奶日期,合计 from " & Form6.Combo1.Text & " where 奶户 = '" & Combo1.Text & _
"' and 挤奶日期 between '" _
& DTPicker1.Value & "' and '" & DTPicker2.Value & "'")                
End If
Set DataGrid1.DataSource = rsb = 0
 For i = 0 To rs.RecordCount - 1
b = b + DataGrid1.Columns(5).CellValue(DataGrid1.RowBook(i))
  Next i
Label7.Caption = b
Set rs = Nothing
Set conn = Nothing
End Sub
command3按钮为"查询结果导出到EXCEL".代码该怎么写.哪位大哥大姐不吝赐教.谢谢

解决方案 »

  1.   

    参考我的模块,很简单
    http://blog.csdn.net/hwmys/archive/2009/12/12/4992773.aspx
      

  2.   

    Private Sub sub_ExpToExcel()
        Dim xlapp As Excel.Application
        Dim xlbook As Excel.Workbook
        Dim xlsheet As Excel.Worksheet    Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim i As Integer    cn.CursorLocation = adUseClient
        cn.Open ""    rs.Open "select * from mytable", cn, adOpenStatic, adLockOptimistic
        
        If rs.RecordCount > 0 Then
            Set xlapp = CreateObject("Excel.Application")
            Set xlbook = xlapp.Workbooks.Add
            Set xlsheet = xlbook.Worksheets(1)
            
            For i = 1 To rs.Fields.Count
                xlsheet.Cells(1, i) = rs.Fields(i - 1).Name
            Next i
            
            xlsheet.Range("a2").CopyFromRecordset rs        xlsheet.SaveAs strFileName
        End If
        
        rs.Close
        Set rs = Nothing
        
        cn.Close
        Set cn = Nothing
    End Sub
      

  3.   

    用 滴答表格控件 吧
    可以将表格上的数据原样导到EXCEL中,包括公式和图片,
    只需要一条代码!
    下载地址:www.jjcsoft.com.cn/download/didasg_setup.rar
      

  4.   

    If Adodc1.Recordset.RecordCount > 0 Then
          For j = 0 To DataGrid1.Columns.count - 1
            newsheet.Cells(1, j + 1) = DataGrid1.Columns(j).Caption
          Next
          Adodc1.Recordset.MoveFirst
       Do Until Adodc1.Recordset.EOF
             r = Adodc1.Recordset.AbsolutePosition
          For c = 0 To DataGrid1.Columns.count - 1
            DataGrid1.Col = c
            newsheet.Cells(r + 1, c + 1) = DataGrid1.Columns(c)
          Next
          Adodc1.Recordset.MoveNext
       Loop