MSHFlexGrid控件中的数据怎样导入Excel中
2个回答
展开全部
1、用数组方式快速导出MSFlexGrid表格数据到Excel表格中
Public Sub ExportToExcel(ByRef objGrid As MSHFlexGrid, ByVal strFileName As String, Optional StartRow As Long = 1, Optional StartColumn As Long = 1)
Dim objApp As Object
Dim objWorkbook As Object
Dim objWorksheet As Object
Dim objRange As Object
Dim CellsData() As String
Dim i As Long, j As Long
Dim nRows As Long, nColumns As Long
'构造二维数组
nRows = objGrid.Rows
nColumns = objGrid.Cols
ReDim CellsData(1 To nRows, 1 To nColumns)
For i = 1 To nRows
For j = 1 To nColumns
CellsData(i, j) = objGrid.TextMatrix(i - 1, j - 1)
Next
Next
'导出到Excel中
If StartRow < 1 Then StartRow = 1
If StartColumn < 1 Then StartColumn = 1
Set objApp = CreateObject("Excel.Application")
objApp.ScreenUpdating = False '禁止屏幕刷新
Set objWorkbook = objApp.Workbooks.Add
Set objWorksheet = objWorkbook.Sheets.Add
Set objRange = objWorksheet.Range(objWorksheet.Cells(StartRow, StartColumn), objWorksheet.Cells((StartRow - 1) + nRows, (StartColumn - 1) + nColumns))
objRange.Value = CellsData
objWorkbook.SaveAs strFileName '保存到指定文件
objWorkbook.Close
objApp.Quit '退出Excel
Set objRange = Nothing
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objApp = Nothing
'销毁二维数组
Erase CellsData
End Sub
2、测试代码如下:
Option Explicit
Private Sub Form_Load()
Dim i As Long, j As Long
'填充测试用数据
Me.MSHFlexGrid1.Rows = 2000
Me.MSHFlexGrid1.Cols = 10
For i = 0 To Me.MSHFlexGrid1.Rows - 1
For j = 0 To Me.MSHFlexGrid1.Cols - 1
Me.MSHFlexGrid1.TextMatrix(i, j) = i & "行" & j & "列"
Next
Next
Debug.Print Me.MSHFlexGrid1.TextArray(100)
End Sub
Private Sub cmdExport_Click()
ExportToExcel Me.MSHFlexGrid1, "d:/temp.xls"
Me.SetFocus
MsgBox "导出完毕"
End Sub
3、测试环境vb6企业版,excel2003
展开全部
'这是我自己的程序中的一段,能导出到EXCEL模板中,进行打印,等下下我改得清楚一点再贴上来
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
以下是精简后的代码,不清楚你工作中的一些细节,所以如有问题与我讨论
Private Sub Command1_Click()
MSFlexGrid1.Redraw = False '关闭表格重画,加快运行速度
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(App.Path & "\对账模板.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlsheet = xlBook.Worksheets("Sheet1") '设置活动工作表
For R = 0 To MSFlexGrid1.Rows - 1 '行循环
For C = 0 To MSFlexGrid1.Cols - 1 '列循环
MSFlexGrid1.Row = R
MSFlexGrid1.Col = C
xlBook.Worksheets("Sheet1").Cells(R + 1, C + 1) = MSFlexGrid1.Text '保存到EXCEL
Next C
Next R
MSFlexGrid1.Redraw = True
'xlsheet.PrintOut '打印工作表
xlApp.DisplayAlerts = False '不进行安全提示
'xlBook.Close (False) '关闭工作簿
Set xlsheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
以下是精简后的代码,不清楚你工作中的一些细节,所以如有问题与我讨论
Private Sub Command1_Click()
MSFlexGrid1.Redraw = False '关闭表格重画,加快运行速度
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(App.Path & "\对账模板.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlsheet = xlBook.Worksheets("Sheet1") '设置活动工作表
For R = 0 To MSFlexGrid1.Rows - 1 '行循环
For C = 0 To MSFlexGrid1.Cols - 1 '列循环
MSFlexGrid1.Row = R
MSFlexGrid1.Col = C
xlBook.Worksheets("Sheet1").Cells(R + 1, C + 1) = MSFlexGrid1.Text '保存到EXCEL
Next C
Next R
MSFlexGrid1.Redraw = True
'xlsheet.PrintOut '打印工作表
xlApp.DisplayAlerts = False '不进行安全提示
'xlBook.Close (False) '关闭工作簿
Set xlsheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询