请教一个问题,如何将Datagrid中的数据导出到EXCEL文件中

lang8848 2004-03-31 05:20:13
我以前将数据导出到EXCEL中需要指定一条SQL查询语句,现在我想直接将datagrid中的数据库导到EXCEL文件中,不知那位高手可解决此问题.
...全文
58 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AKillGodKillBuddha 2004-06-23
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/community/Column/24.mspx
AKillGodKillBuddha 2004-06-23
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/read_article.asp?id=14952
AKillGodKillBuddha 2004-06-23
  • 打赏
  • 举报
回复
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen1 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)



'On Error GoTo excle
With Rs_Temp
.MoveLast

If .RecordCount < 1 Then
MsgBox ("没有记录!")
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
'在Excel中的第一行加标题
Case 1
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
'将数组FIELDLEN()存为第一条记录的字段长
Case 2
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
If IsNull(.Fields(Icol - 1)) Then
Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
Else
Fieldlen1 = LenB(.Fields(Icol - 1))
End If

If Fieldlen(Icol) < Fieldlen1 Then
xlSheet.Columns(Icol).ColumnWidth = IIf(Fieldlen1 > 255, 255, Fieldlen1)
'表格列宽等于较长字段长
Fieldlen(Icol) = IIf(Fieldlen1 > 255, 255, 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 > 2 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

'*!* 页眉、填报单位、报表时间、单位
With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,常规""业务数据综合查询表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With

'显示表格
Dim ExclFileName As String
ExclFileName = App.path & "\业务数据综合查询表.xls"
If Dir(ExclFileName) <> "" Then
Kill ExclFileName
End If
xlSheet.SaveAs (ExclFileName)

xlApp.Application.Visible = True
'交还控制给Excel
'xlSheet.PrintPreview
'xlApp.Quit
End With
yujiang930 2004-04-01
  • 打赏
  • 举报
回复
fuanwei(fuanwei) 没有说清楚啊,语句有问题啊。能不能在详细点?

kissoflife(明月高楼休独倚,酒入愁肠,化作相思泪!) 能不能给个例子啊?小弟初学,麻烦了啊!
wumy_ld 2004-04-01
  • 打赏
  • 举报
回复
With xlSheet
For i = 1 To 6
.Range(.Cells(1, i), .Cells(2, i)).MergeCells = True
Next
.Cells(1, 1).Value = "贷款号"
.Cells(1, 2).Value = "项目名称"
.Cells(1, 3).Value = "币种"
.Cells(1, 4).Value = "累计到期" & vbCrLf & "应还本金"
.Cells(1, 5).Value = "累计到期" & vbCrLf & "应还利息"
.Cells(1, 6).Value = "合计"

.Range(.Cells(1, 7), .Cells(1, 9)).MergeCells = True
.Cells(1, 7).Value = "对财政部累计到期欠款余额"
.Cells(2, 7).Value = "本金"
.Cells(2, 8).Value = "利息"
.Cells(2, 9).Value = "合计"

.Range(.Cells(1, 10), .Cells(1, 11)).MergeCells = True
.Cells(1, 10).Value = "其中:"
.Cells(2, 10).Value = "上年末累计欠款"
.Cells(2, 11).Value = "本年新增欠款"

'以下代码填充实际数据
.Range(.Cells(9, 1), .Cells(10, 1)).MergeCells = True
.Cells(9, 1).Value = "合计"
.Cells(9, 3).Value = "USD"
.Cells(10, 3).Value = "RMB"
End With

就是像上面这样一个一个格的填充。
liuminhui1 2004-03-31
  • 打赏
  • 举报
回复
用剪贴板拷贝,直接拷入内存中,在贴到EXEL
这样做很快如果需要的话,我这里做了一个控件
QQ:38647302
wumy_ld 2004-03-31
  • 打赏
  • 举报
回复
比较笨的办法,逐个写入
fuanwei 2004-03-31
  • 打赏
  • 举报
回复
cn.Execute "SELECT * INTO [Excel 8.0;DATABASE=" 路径及文件名 from 消费单据表 where 查询条件"

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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