我想吧我查询到得在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".代码该怎么写.哪位大哥大姐不吝赐教.谢谢
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".代码该怎么写.哪位大哥大姐不吝赐教.谢谢
解决方案 »
- vb中用ado读取csv文件时,recordset无法打开,提示参数太少,急。在线等
- 想用Ado 创建一个Access表,但运行时说"字段定义语法错误"
- 请诸位高手,帮个忙,给小弟指点一下,
- adodc1.recordset.addnew后,想撤消这个动作,怎么实现?
- 求监视目录的代码,最好是时间响应的
- RptTextBox如何动态绑定DE中的字段? 急需,谢谢,在线等待!
- 我想得到DATAGRID里的行数该怎么做?
- 为什么SQL语句中<>'某项' 返回的结果不对
- 如何用SQL语句删除一个字段
- 关于数据库刷新的问题??时间比较急,请各位大虾指教!!!
- 在线等待,循环,随即,WebBrowser问题
- vb winsock 通过网页获取彩票动态开奖号码
http://blog.csdn.net/hwmys/archive/2009/12/12/4992773.aspx
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
可以将表格上的数据原样导到EXCEL中,包括公式和图片,
只需要一条代码!
下载地址:www.jjcsoft.com.cn/download/didasg_setup.rar
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