'以下代码为将mshflexgrid中的数据导出到excel文件中   Dim i_row as integer
  dim i_col As Integer
  '创建EXCEL应用对象,以及相关的EXCEL对象并引用
  Set xlapp = CreateObject("excel.application")
  xlapp.Visible = True
  Set xlbook = xlapp.workbooks.Add
  Set xlsheet = xlbook.worksheets(1)
  i_row = 0
  Do While i_row < mshflexgrid.Rows
     For i_col = 0 To mshflexgrid.Cols - 1
         xlsheet.cells(i_row, i_col + 1) = mshflexgrid.TextMatrix (i_row, i_col)
     Next i_col
     i_row = i_row + 1
  Loop

解决方案 »

  1.   

    不知道听说谁的,印象中好像不用VBA也可以实现
    好像是存成文本格式,其中表格数据用逗号(可能是,忘了,也可能是tab()之类的函数)分开,然后存成.xls格式,打开时,会有提示的。不过我做了没这个效果,不知道这样能不能实现?
      

  2.   

    Dim excelApp As Excel.Application
        Set excelApp = New Excel.Application
        On Error Resume Next
        If excelApp Is Nothing Then
           Set excelApp = CreateObject("Excel.application")
           If excelApp Is Nothing Then
              Exit Sub
           End If
        End If
        excelApp.Visible = True
        Me.MousePointer = vbHourglass
        excelApp.Workbooks.Add
        With excelApp.ActiveSheet
            Dim i As Integer, j As Integer
            For i = 1 To MSFlexGrid1.Rows
                For j = 1 To MSFlexGrid1.Cols
                      .Cells(i, j).Value = MSFlexGrid1.TextMatrix((i - 1), (j - 1))
                    
                Next j
                DoEvents
            Next i
                End With
        Me.MousePointer = vbDefault
        Set excelApp = Nothing
      

  3.   

    谢谢,VBA我还知道一二。就是想明白我说的那样的,有没有办法实现?