请教各位高手,如何编程序,在vb程序运行的时候,将DataGrid控件中的数据导入到excel中去?

baoxue10181018 2003-08-22 02:01:28
我的dataGrid控件的名称为DataGrid1,DataGrid1中显示的是搜索到的满足条件的记录.
我希望在点击按钮command1时将数据导入到excel中去,请高手指点!谢谢!
...全文
53 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
neo40 2003-08-22
  • 打赏
  • 举报
回复
‘无责任导出’哈哈
neo40 2003-08-22
  • 打赏
  • 举报
回复
不好意思落一句

Dim ss As New ADODB.Connection
ss.ConnectionString = "连接字符串(自己添)"
ss.Open
ss.Execute "select 字段1,字段2 into [Excel 8.0;DATABASE=" + App.Path + "\myExcel.xls].[WorkSheet1] from 表 "
neo40 2003-08-22
  • 打赏
  • 举报
回复
Dim ss As New ADODB.Connection
ss.ConnectionString = "连接字符串(自己添)"
ss.Execute "select 字段1,字段2 into [Excel 8.0;DATABASE=" + App.Path + "\myExcel.xls].[WorkSheet1] from 表 "

即方便又快捷的方法(可能是所有方法中最快的),接分中
baoxue10181018 2003-08-22
  • 打赏
  • 举报
回复
谢谢!
lihonggen0 2003-08-22
  • 打赏
  • 举报
回复


引用Microsoft Excel类型库:

从"工程"菜单中选择"引用"栏;

选择Microsoft Excel 8.0 Object Library;

选择"确定"。

在按钮的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 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


lihonggen0 2003-08-22
  • 打赏
  • 举报
回复
要求excel 2000以上

excel 97只能一个一个单元格地写入
baoxue10181018 2003-08-22
  • 打赏
  • 举报
回复
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))
请问这句为什么总出错?
lihonggen0 2003-08-22
  • 打赏
  • 举报
回复
这个是你的数据库连接
wangyanqiu0918 2003-08-22
  • 打赏
  • 举报
回复
我是一名新手,我用了上面的方法,可是在运行给的那段程序的时候。这一句老是出问题你能告诉我怎么办吗?谢谢! “.ActiveConnection = Cn“
lihonggen0 2003-08-22
  • 打赏
  • 举报
回复
这个问题问了许多次了,

dataGrid的数据源就是一个记录集Rs
将Rs导出到excel就可以了

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

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧