在VB中把EXCEL文件导入MSFLEXGRID 之二

来源:百度文库 编辑:神马文学网 时间:2024/04/28 14:57:53
首先需要引用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