vb中的datagrid里面的数据如何导出到excel表里面
请写详细点,谢谢各位大神!!!我在线等!!分数不够我还可以追加,急啊!!Dima,bAsStringDimtxtsqlAsStringPrivateSubCommand1...
请写详细点,谢谢各位大神!!!我在线等!!分数不够我还可以追加,急啊!!
Dim a, b As String
Dim txtsql As String
Private Sub Command1_Click()
b = Option2.Value
Dim rs0 As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
b = True
txtsql = "select 队名,地类, sum(面积数) as 总数 from 包厢 group by 队名,地类"
rs0.CursorLocation = adUseClient
rs0.Open txtsql, cn, adOpenKeyset, adLockPessimistic
Set DataGrid1.DataSource = rs0
rs0.Requery
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
这是我界面确定按钮的代码,当我点确定的时候,统计数据显示在datagrid里面
我的意思是想点command3的时候统计数据就导出到excel表中去,该怎么弄啊!!
请各位大神帮忙!! 展开
Dim a, b As String
Dim txtsql As String
Private Sub Command1_Click()
b = Option2.Value
Dim rs0 As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
b = True
txtsql = "select 队名,地类, sum(面积数) as 总数 from 包厢 group by 队名,地类"
rs0.CursorLocation = adUseClient
rs0.Open txtsql, cn, adOpenKeyset, adLockPessimistic
Set DataGrid1.DataSource = rs0
rs0.Requery
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
这是我界面确定按钮的代码,当我点确定的时候,统计数据显示在datagrid里面
我的意思是想点command3的时候统计数据就导出到excel表中去,该怎么弄啊!!
请各位大神帮忙!! 展开
3个回答
展开全部
这个其实很简单的,步骤如下:
首先在你的工程中引用Excel对象,比如“Microsoft Excel 11 Object Library”;
然后在执行导出的代码里声明对象(代码仅供参考):
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strFilename as String ''Excel文件
Dim i as Integer,j as Integer
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = False '设置EXCEL不可见
Set xlBook = xlApp.Workbooks.Open(strFilename) '打开EXCEL工作簿
Set xlSheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlSheet.Activate '激活工作表
xlBook.RunAutoMacros (xlAutoOpen) ''运行EXCEL中的启动宏
''写入数据到Excel
For i=1 to 100
For j=1 to 100
xlSheet.Cells(i, j) = "i=" & cstr(i) & ";j=" & cstr(j)
Next
Next
'大功告成,关闭对象以及退出
xlBook.Save ‘''保存关闭文档
xlBook.RunAutoMacros (xlAutoClose) '关闭宏
xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
首先在你的工程中引用Excel对象,比如“Microsoft Excel 11 Object Library”;
然后在执行导出的代码里声明对象(代码仅供参考):
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strFilename as String ''Excel文件
Dim i as Integer,j as Integer
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = False '设置EXCEL不可见
Set xlBook = xlApp.Workbooks.Open(strFilename) '打开EXCEL工作簿
Set xlSheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlSheet.Activate '激活工作表
xlBook.RunAutoMacros (xlAutoOpen) ''运行EXCEL中的启动宏
''写入数据到Excel
For i=1 to 100
For j=1 to 100
xlSheet.Cells(i, j) = "i=" & cstr(i) & ";j=" & cstr(j)
Next
Next
'大功告成,关闭对象以及退出
xlBook.Save ‘''保存关闭文档
xlBook.RunAutoMacros (xlAutoClose) '关闭宏
xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
展开全部
先保存到DataSet中,然后再保存到Excel中
追问
能具体点吗,大神,如何操作,能麻烦你给点代码吗!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果想把datagrid里的数据打印出来怎么实现
方法太多了,可以直接打印窗体,可以用水晶报表,可以调用printer,但如果你把它导出到Excel,比上面的方案都好。
导出到EXCEL要用到excel程序提供的Application对像
使用前先在 工程 --引用 里添加Microsoft excel.....的引用。
然后定义 application...
还是给你看一段代码吧。希望对你有帮助
控件 adodc+datagrid+command
Private Sub cmdExport_Click()
Dim i As Integer, r As Integer, c As Integer
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
Set newxls = CreateObject("Excel.Application") '创建excel应用程序,打开excel2000
Set newbook = newxls.Workbooks.Add '创建工作簿
Set newsheet = newbook.Worksheets(1) '创建工作表
If sql > "" Then
Adodc1.RecordSource = sql
Adodc1.Refresh
End If
If Adodc1.Recordset.RecordCount > 0 Then
For i = 0 To DataGrid1.Columns.Count - 1
newsheet.Cells(1, i + 1) = DataGrid1.Columns(i).Caption
Next i
'指定表格内容
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 c
Adodc1.Recordset.MoveNext
Loop
Dim myval As Long
Dim mystr As String
myval = MsgBox("是否保存该Excel表?", vbYesNo, "提示窗口")
If myval = vbYes Then
mystr = InputBox("请输入文件名称", "输入窗口")
If Len(mystr) = 0 Then
MsgBox "系统不允许文件名称为空!", , "提示窗口"
Exit Sub
End If
On Error GoTo ErrSave
newsheet.SaveAs App.Path & "\Excel文件\" & mystr & ".xls"
MsgBox "Excel文件保存成功,位置:" & App.Path & "\Excel文件\" & mystr & ".xls", , "提示窗口"
newxls.Quit
ErrSave:
Exit Sub
MsgBox Err.Description, , "提示窗口"
End If
End If
End Sub
补充回答:因为路径不丰在,换一个存在的路径,不行就改成 App.Path & mystr & ".xls" 之类
方法太多了,可以直接打印窗体,可以用水晶报表,可以调用printer,但如果你把它导出到Excel,比上面的方案都好。
导出到EXCEL要用到excel程序提供的Application对像
使用前先在 工程 --引用 里添加Microsoft excel.....的引用。
然后定义 application...
还是给你看一段代码吧。希望对你有帮助
控件 adodc+datagrid+command
Private Sub cmdExport_Click()
Dim i As Integer, r As Integer, c As Integer
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
Set newxls = CreateObject("Excel.Application") '创建excel应用程序,打开excel2000
Set newbook = newxls.Workbooks.Add '创建工作簿
Set newsheet = newbook.Worksheets(1) '创建工作表
If sql > "" Then
Adodc1.RecordSource = sql
Adodc1.Refresh
End If
If Adodc1.Recordset.RecordCount > 0 Then
For i = 0 To DataGrid1.Columns.Count - 1
newsheet.Cells(1, i + 1) = DataGrid1.Columns(i).Caption
Next i
'指定表格内容
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 c
Adodc1.Recordset.MoveNext
Loop
Dim myval As Long
Dim mystr As String
myval = MsgBox("是否保存该Excel表?", vbYesNo, "提示窗口")
If myval = vbYes Then
mystr = InputBox("请输入文件名称", "输入窗口")
If Len(mystr) = 0 Then
MsgBox "系统不允许文件名称为空!", , "提示窗口"
Exit Sub
End If
On Error GoTo ErrSave
newsheet.SaveAs App.Path & "\Excel文件\" & mystr & ".xls"
MsgBox "Excel文件保存成功,位置:" & App.Path & "\Excel文件\" & mystr & ".xls", , "提示窗口"
newxls.Quit
ErrSave:
Exit Sub
MsgBox Err.Description, , "提示窗口"
End If
End If
End Sub
补充回答:因为路径不丰在,换一个存在的路径,不行就改成 App.Path & mystr & ".xls" 之类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |