分享

在VB中把EXCEL文件导入MSFLEXGRID

 a_cheng 2009-11-12
首先需要引用Excel库:

从"工程"菜单中选择"引用"栏;选择Microsoft Excel 11.0 Object Library(EXCEL2003),然后选择"确定"。表示在工程中要引用EXCEL类型库。



Private Sub Command1_Click()

Dim ExcelApp As Excel.Application

Set ExcelApp = CreateObject("excel.application")

ExcelApp.Workbooks.Open (App.Path & " est2.xls")

With MSFlexGrid1

.Rows = ExcelApp.Sheets(1).UsedRange.Rows.Count

.Cols = 4

For r = 0 To .Rows - 1

For c = 1 To .Cols

If c = 1 Then

.TextMatrix(r, c - 1) = Year(Date) & "-" & ExcelApp.Sheets(1).Cells(r + 1, c + 1) & "-" & ExcelApp.Sheets(1).Cells(r + 1, c)

Else

.TextMatrix(r, c - 1) = ExcelApp.Sheets(1).Cells(r + 1, c + 1)

End If

Next

Next

End With

ExcelApp.Quit

End Sub



Private Sub Command2_Click()

Open App.Path & "导出.txt" For Output As #1

With MSFlexGrid1

For r = 0 To .Rows - 1

For c = 0 To .Cols - 1

Print #1, .TextMatrix(r, c);

If c < .Cols - 1 Then Print #1, ",";

Next

Print #1,

Next

End With

Close #1

MsgBox "导出完毕"

End Sub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多