加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB将查询显示在MSHFlexGrid或MSFlexGrid控件中的数据导出到Excel中

(2018-10-17 17:04:51)
一、导出并打开新增的Excel文件,然后再手动保存。
Private Sub Command1_Click() '将查询数据导出到新增的Excel表里
    Dim xlsApp    As Excel.Application '添加工程-引用"Microsoft Excel 11.0 Object Library"才能使用
    Dim xlsBook   As Excel.Workbook
    Dim xlsSheet  As Excel.Worksheet
    Dim i         As Integer
    Dim j         As Integer
    Set xlsApp = New Excel.Application
    Set xlsBook = xlsApp.Workbooks.Add
    Set xlsSheet = xlsBook.Worksheets(1)
        xlsSheet.Cells(1, 1) = "水库水位监测数据"
    With Form5.MSHFlexGrid1  '或MSFlexGrid1
    For i = 0 To .Rows - 1
        For j = 1 To 1.Cols - 1
            xlsSheet.Cells(i + 2, j) =.TextMatrix(i, j)
        Next
    Next
    End with
    xlsApp.Visible = True '对象可见
    Set xlsSheet = Nothing
    Set xlsBook = Nothing
    Set xlsApp = Nothing
End Sub
二、导出到指定的Excel文件中并保存。
Private Sub Command1_Click() '将查询数据导出到已有的Excel的99.xls表中
    Dim xlsApp As Object
    Dim xlsBook As Object
    Dim Fname$, i&, j&
    Set xlsApp = CreateObject("Excel.Application")  '创建excel对象
    xlsApp.Visible = True                           '对象可见
    xlsApp.DisplayAlerts = True                     '关闭时提示保存
    Set xlsBook = xlsApp.Workbooks.Open(App.Path & "\99.xls")      '打开文件
    If Val(xlsApp.Application.Version) >= 8 Then
       Set xlsBook = xlsApp.ActiveSheet
    Else
       Set xlsBook = xlsApp
    End If
    xlsApp.Sheets("Sheet1").Select
    For i = 0 To MSHFlexGrid1.Rows - 1
       For j = 0 To MSHFlexGrid1.Cols - 1
          xlsApp.Cells(i + 1, j + 1) = MSHFlexGrid1.TextMatrix(i, j)
       Next
    Next
    xlsBook.SaveAs (App.Path & "\99.xls")
    xlsApp.Quit
    Set xlsBook = Nothing
    Set xlsApp = Nothing
End Sub

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有