
3个回答
展开全部
你好,给你提供函数,非常容易的做到:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Function ExportToExcel(objMsg As MSFlexGrid) As Boolean
'*********************************************************************
' 将MSFlexGrid的查询结果保存到Excel里。调用 CallExportToExcel(objMsg As MSFlexGrid)
' 完全可以保存查询的结果到Excel里。
'Purpose: Export data on grid to an Excel file
'Input :
'Output : True if successful, else return False
'Others : Global variable used:
' Module variable used:
'********************************************************************
On Error GoTo ExportToExcel_ErrorHandler
Dim objExcelApp As Object
Dim objExcelBook As Object
Dim objExcelSheet As Object
'Whether Excel exists, if not, try to create
On Error Resume Next
Set objExcelApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcelApp = CreateObject("Excel.Application")
End If
On Error GoTo ExportToExcel_ErrorHandler
Set objExcelBook = objExcelApp.Workbooks.Add
Set objExcelSheet = objExcelBook.Worksheets(1)
If Val(objExcelApp.Application.Version) >= 8 Then
Set objExcelSheet = objExcelApp.ActiveSheet
Else
Set objExcelSheet = objExcelApp
End If
Dim lngRowsCount As Long, lngColumnsCount As Long, lngRow As Long, lngColumn As Long
Dim strText As String
lngRowsCount = objMsg.Rows
lngColumnsCount = objMsg.Cols
For lngRow = 1 To lngRowsCount
For lngColumn = 1 To lngColumnsCount
strText = objMsg.TextMatrix(lngRow - 1, lngColumn - 1)
If IsNull(strText) = False And strText <> "" Then
objExcelSheet.Cells(lngRow, lngColumn) = strText
End If
Next
Next
objExcelApp.Visible = True
Set objExcelSheet = Nothing
Set objExcelBook = Nothing
Set objExcelApp = Nothing
ExportToExcel = True
ErrorHandler:
Exit Function
ExportToExcel_ErrorHandler:
MsgBox Err.Description
Resume ErrorHandler
End Function
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Function ExportToExcel(objMsg As MSFlexGrid) As Boolean
'*********************************************************************
' 将MSFlexGrid的查询结果保存到Excel里。调用 CallExportToExcel(objMsg As MSFlexGrid)
' 完全可以保存查询的结果到Excel里。
'Purpose: Export data on grid to an Excel file
'Input :
'Output : True if successful, else return False
'Others : Global variable used:
' Module variable used:
'********************************************************************
On Error GoTo ExportToExcel_ErrorHandler
Dim objExcelApp As Object
Dim objExcelBook As Object
Dim objExcelSheet As Object
'Whether Excel exists, if not, try to create
On Error Resume Next
Set objExcelApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcelApp = CreateObject("Excel.Application")
End If
On Error GoTo ExportToExcel_ErrorHandler
Set objExcelBook = objExcelApp.Workbooks.Add
Set objExcelSheet = objExcelBook.Worksheets(1)
If Val(objExcelApp.Application.Version) >= 8 Then
Set objExcelSheet = objExcelApp.ActiveSheet
Else
Set objExcelSheet = objExcelApp
End If
Dim lngRowsCount As Long, lngColumnsCount As Long, lngRow As Long, lngColumn As Long
Dim strText As String
lngRowsCount = objMsg.Rows
lngColumnsCount = objMsg.Cols
For lngRow = 1 To lngRowsCount
For lngColumn = 1 To lngColumnsCount
strText = objMsg.TextMatrix(lngRow - 1, lngColumn - 1)
If IsNull(strText) = False And strText <> "" Then
objExcelSheet.Cells(lngRow, lngColumn) = strText
End If
Next
Next
objExcelApp.Visible = True
Set objExcelSheet = Nothing
Set objExcelBook = Nothing
Set objExcelApp = Nothing
ExportToExcel = True
ErrorHandler:
Exit Function
ExportToExcel_ErrorHandler:
MsgBox Err.Description
Resume ErrorHandler
End Function
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
采用引用的方式最方便
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
打开Excel按MSFLEXGRID循环到入
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询