机房收费系统—导出excel

机房收费系统中导出excel是从来没有接触过的知识,下面就来介绍一下方法。

一:直接引用VB中自带的Microsoft Excel 14.0 Object Library

Private Sub Excel_Click()                                           '将数据导出到excel

    Dim i As Integer
    Dim j As Integer


    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet


    '对象实例化,使其可见
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    '实例化工作簿和表单
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    '将MSHFLEXGRID中的数据导入到excel
    For i = 0 To myflexgrid.Rows - 1
        For j = 0 To myflexgrid.Cols - 1
            myflexgrid.Row = i
            myflexgrid.Col = j
            xlSheet.Cells(i + 1, j + 1) = Trim(myflexgrid.Text)
        Next
    Next
End Sub
二:在根目录中事先建立空的Excel表格

1在与VB工程同一个根目录中建立将要导入数据的空excel表格

2在VB中写代码

Private Sub Command3_Click()
        '导出数据
    Dim i As Integer
    Dim j As Integer
    myflexgrid.Redraw = False '关闭表格重画
    
    Set xlApp = CreateObject("Excel.Application")  '创建EXCEL对象
    '打开已经存在的excel工作薄文件
    Set xlBook = xlApp.Workbooks.Open(App.Path & "\新建 Microsoft Excel 工作表 (2).xlsx")
    xlApp.Visible = True '设置excel对象可见
    
    Set xlSheet = xlBook.Worksheets("Sheet1")  '设置活动工作表
    
    For i = 0 To myflexgrid.Rows - 1 '行循环
        For j = 0 To myflexgrid.Cols - 1 '列循环
            myflexgrid.Row = i
            myflexgrid.Col = j
            '保存到EXCEL
        xlBook.Worksheets("Sheet1").Cells(i + 1, j + 1) = myflexgrid.Text
        Next j
    Next i
        myflexgrid.Redraw = True
    
End Sub
这样既可以导出你要导出的数据了。


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/n950814abc/article/details/52355312
所属专栏: 机房收费系统
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭