VB中如何将msflexgrid控件中的数据导成Excel
1个回答
展开全部
在机房收费系统中,其中有一个功能是:将Msflexgrid控件中的数据导入到excel中,经过几天的奋战,连查带改,现在提供一种方法,仅供参考:
实现这个功能,首先需先在工程中引用MrcrosoftExcel xx Object Library,然后新建一个模块,声明如下子过程:[vb]
Public SubOutDataToExcel(Flex AsMSFlexGrid) '导出至Excel
Dim s As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
OnError GoTo Ert
Me.MousePointer = 11
Dim Excelapp As Excel.Application
Set Excelapp = New Excel.Application
OnError Resume NextDoEvents
Excelapp.SheetsInNewWorkbook = 1
Excelapp.Workbooks.Add
Excelapp.ActiveSheet.Cells(1, 3) = s
Excelapp.Range(C1).Select
Excelapp.Selection.Font.FontStyle=Bold
Excelapp.Selection.Font.Size = 16With Flexk= .Rows
For i = 0 To k - 1
For j = 0 To .Cols - 1DoEvents
Excelapp.ActiveSheet.Cells(3 + i, j + 1) =' &.TextMatrix(i, j)Next jNext iEnd With
Me.MousePointer = 0
Excelapp.Visible = True
Excelapp.Sheets.PrintPreviewErt:
If Not (Excelapp Is Nothing) Then
Excelapp.QuitEnd IfEnd Sub
调用该子过程直接呈现的是EXCEL打印预览界面,如果需要编辑,把上面子过程中的Excelapp.Sheets.PrintPreview 和 Excelapp.Quit 这二条语句去掉,这样就会直接显示导入数据后的Excel表.
然后在命令按钮单击事件下编写如下代码:[vb]
实现这个功能,首先需先在工程中引用MrcrosoftExcel xx Object Library,然后新建一个模块,声明如下子过程:[vb]
Public SubOutDataToExcel(Flex AsMSFlexGrid) '导出至Excel
Dim s As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
OnError GoTo Ert
Me.MousePointer = 11
Dim Excelapp As Excel.Application
Set Excelapp = New Excel.Application
OnError Resume NextDoEvents
Excelapp.SheetsInNewWorkbook = 1
Excelapp.Workbooks.Add
Excelapp.ActiveSheet.Cells(1, 3) = s
Excelapp.Range(C1).Select
Excelapp.Selection.Font.FontStyle=Bold
Excelapp.Selection.Font.Size = 16With Flexk= .Rows
For i = 0 To k - 1
For j = 0 To .Cols - 1DoEvents
Excelapp.ActiveSheet.Cells(3 + i, j + 1) =' &.TextMatrix(i, j)Next jNext iEnd With
Me.MousePointer = 0
Excelapp.Visible = True
Excelapp.Sheets.PrintPreviewErt:
If Not (Excelapp Is Nothing) Then
Excelapp.QuitEnd IfEnd Sub
调用该子过程直接呈现的是EXCEL打印预览界面,如果需要编辑,把上面子过程中的Excelapp.Sheets.PrintPreview 和 Excelapp.Quit 这二条语句去掉,这样就会直接显示导入数据后的Excel表.
然后在命令按钮单击事件下编写如下代码:[vb]
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询