Public Sub SaveGrid(ByVal strHead As String, ByVal strTail As String, GridName As MSFlexGrid, ByVal Cmdiag As CommonDialog)
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim CmDiag1 As CommonDialog
Dim Loopi As Integer
Dim Loopj As Integer
Dim fileName As String
' Set CmDiag1 = Cmdiag
With Cmdiag
.fileName = ""
.DialogTitle = "输出到文件"
.CancelError = False
'设置 common dialog 控件的标志和属性
.Filter = "Mirosoft Excel 文件 (*.XLS)|*.xls"
.ShowSave
If .CancelError Then Exit Sub
If Len(.fileName) = 0 Then
Exit Sub
End If
fileName = .fileName
End With
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(0)
With GridName
xlSheet.Cells(1, 1) = strHead
For Loopi = 0 To .Rows - 1
For Loopj = 0 To .Cols - 1
xlSheet.Cells(Loopi + 1, Loopj + 1) = .TextMatrix(Loopi, Loopj)
Next Loopj
Next Loopi
End With
xlSheet.Cells(Loopi + 2, 1) = strTail
On Error GoTo Err
xlBook.SaveAs fileName
xlBook.Close False
Set xlBook = Nothing
Set xlApp = Nothing
MsgBox "成功输出", vbInformation + vbOKOnly, "输出"
Exit Sub
Err:
If Err <> 0 Then
MsgBox "导出数据失败,错误描述:" & Err.Number & ":" & Err.Description, vbOKOnly
' Resume Next
End If
DoEvents
End Sub
Public Sub InputExl(ByVal GridName As MSFlexGrid, ByVal Cmdiag As CommonDialog)
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim fileName As String
Dim Loopi As Integer
Dim Loopj As Integer
With Cmdiag
.fileName = ""
.DialogTitle = "选择输入文件"
.CancelError = False
'设置 common dialog 控件的标志和属性
.Filter = "Mirosoft Excel 文件 (*.XLS)|*.xls"
.ShowOpen
If Len(.fileName) = 0 Then
Exit Sub
End If
fileName = .fileName
End With
If Trim(Cmdiag.fileName) = "" Then Exit Sub
fileName = Cmdiag.fileName
Set xlBook = xlApp.Workbooks.Open(fileName)
Set xlSheet = xlBook.ActiveSheet
On Error Resume Next
For Loopi = 10 To 27 '这两行是特定程序使用的,你可以做修改 换成其他,或者删除
For Loopj = 2 To 9 '这两行是特定程序使用的,你可以做修改 换成其他,或者删除
If Loopi - 9 >= GridName.Rows Then GridName.Rows = GridName.Rows + 1 '如果GridName的行数小于Excle的行数,则增加一行
GridName.TextMatrix(Loopi - 9, Loopj - 2) = xlSheet.Cells(Loopi, Loopj)
If Loopj - 2 > 1 Then
GridName.TextMatrix(Loopi - 9, Loopj - 2) = Format(GridName.TextMatrix(Loopi - 9, Loopj - 2), "############0.00")
End If
Next