2024年2月22日发(作者:叔觅云)
以下代码写在模块中:
'MSHFlexGrid控件导出到Excel
Public Function ExportFlexDataToExcel(flex As MSFlexGrid, g_CommonDialog As CommonDialog)
On Error GoTo ErrHandler
Dim xlApp As Object
Dim xlBook As Object
Dim Rows As Integer, Cols As Integer
Dim iRow As Integer, hCol As Integer, iCol As Integer
Dim New_Col As Boolean
Dim New_Column As Boolean
g_Error = True
On Error GoTo ErrHandler
' 设置标志
g_ = cdlOFNHideReadOnly
' 设置过滤器
g_ = "All Files (*.*)|*.*|Excel Files" & _
"(*.xls)|*.xls|Batch Files (*.bat)|*.bat"
' 指定缺省的过滤器
g_Index = 2
' 显示“打开”对话框
g_ve
If <= 1 Then
MsgBox "没有数据!", vbInformation, g_Msgtitle
Exit Function
End If
Set xlApp = CreateObject("ation")
Set xlBook =
e = False
With flex
Rows = .Rows
Cols = .Cols
iRow = 0
iCol = 1
For hCol = 0 To Cols - 1
For iRow = 1 To Rows
(iRow, iCol).Value = .TextMatrix(iRow - 1, hCol)
Next iRow
iCol = iCol + 1
Next hCol
End With
With xlApp
.Rows(1). = True
.
.t
.Cells(1, 1).Select
' .e = True
End With
(g_me)
e = False
yAlerts = False
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
us
MsgBox "数据已经导出到Excel中。", vbInformation, "成功"
Exit Function
ErrHandler:
' 用户按了“取消”按钮
If <> 32755 Then
MsgBox "数据导出失败!", vbCritical, "警告"
End If
End Function
在窗体中调用:
ExportFlexDataToExcel Grid1, cdlSelectExcel 'Grid1为MSHFlexGrid控件名,cdlSelectExcel为CommonDialog控件名
2024年2月22日发(作者:叔觅云)
以下代码写在模块中:
'MSHFlexGrid控件导出到Excel
Public Function ExportFlexDataToExcel(flex As MSFlexGrid, g_CommonDialog As CommonDialog)
On Error GoTo ErrHandler
Dim xlApp As Object
Dim xlBook As Object
Dim Rows As Integer, Cols As Integer
Dim iRow As Integer, hCol As Integer, iCol As Integer
Dim New_Col As Boolean
Dim New_Column As Boolean
g_Error = True
On Error GoTo ErrHandler
' 设置标志
g_ = cdlOFNHideReadOnly
' 设置过滤器
g_ = "All Files (*.*)|*.*|Excel Files" & _
"(*.xls)|*.xls|Batch Files (*.bat)|*.bat"
' 指定缺省的过滤器
g_Index = 2
' 显示“打开”对话框
g_ve
If <= 1 Then
MsgBox "没有数据!", vbInformation, g_Msgtitle
Exit Function
End If
Set xlApp = CreateObject("ation")
Set xlBook =
e = False
With flex
Rows = .Rows
Cols = .Cols
iRow = 0
iCol = 1
For hCol = 0 To Cols - 1
For iRow = 1 To Rows
(iRow, iCol).Value = .TextMatrix(iRow - 1, hCol)
Next iRow
iCol = iCol + 1
Next hCol
End With
With xlApp
.Rows(1). = True
.
.t
.Cells(1, 1).Select
' .e = True
End With
(g_me)
e = False
yAlerts = False
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
us
MsgBox "数据已经导出到Excel中。", vbInformation, "成功"
Exit Function
ErrHandler:
' 用户按了“取消”按钮
If <> 32755 Then
MsgBox "数据导出失败!", vbCritical, "警告"
End If
End Function
在窗体中调用:
ExportFlexDataToExcel Grid1, cdlSelectExcel 'Grid1为MSHFlexGrid控件名,cdlSelectExcel为CommonDialog控件名