标签:
首先在VB中加入引用Microsoft Excel 14.0 Object Library。然后找到对应button,在过程中加入例如以下代码:
<pre name="code" class="vb">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
执行后例如以下:
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4282243.html