首先在VB中添加引用Microsoft Excel 14.0 Object library。然后找到相应按钮,在过程中添加如下代码:
<pre name="code" >Private Sub cmdExport_Click() Dim i As Long,j As Long Dim CellsData() As String <span> </span>'定义数组承载数据 'vb中定义excel对象 Dim objApp As Excel.Application Dim objWorkbook As Excel.Workbook Dim objWorksheet As Excel.Worksheet Dim objRange As Excel.Range '构建二维数组,容纳MSHFlexgrID中的内容 ReDim CellsData(1 To myflexgrID.Rows,1 To myflexgrID.Cols) For i = 1 To myflexgrID.Rows For j = 1 To myflexgrID.Cols CellsData(i,j) = myflexgrID.TextMatrix(i - 1,j - 1) Next Next '执行导出到excel中的代码 Set objApp = New Excel.Application <span> </span>'建立xlapp的引用 objApp.ScreenUpdating = False <span> </span>'禁止屏幕刷新 Set objWorkbook = objApp.Workbooks.Add <span> </span>'添加工作簿 Set objWorksheet = objWorkbook.Sheets.Add <span> </span>'添加工作栏 Set objRange = objWorksheet.Range(objWorksheet.Cells(1,1),& _
<span> </span>objWorksheet.Cells(myflexgrID.Rows,myflexgrID.Cols)) objRange.Value = CellsData <span> </span>'excel表中的内容就是数组的内容 objApp.Visible = True <span> </span>'表可见 objApp.ScreenUpdating = True<span> </span>'屏幕可刷新 '销毁二维数组 Erase CellsData Me.SetFocus <span> </span>'窗体获得焦点 MsgBox "导出完毕!",vbOKOnly + vbExclamation,"警告" End Sub
运行后如下:
总结
以上是内存溢出为你收集整理的VB的MSHFlexGrid控件内容导入Excel全部内容,希望文章能够帮你解决VB的MSHFlexGrid控件内容导入Excel所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)