如何将显示在MSHFlexGrid控件中的表格记录导入到excel表中

解决方案 »

  1.   

    先将MSHFlexGrid的数据保存到数据库里面,然后试试:
    Public Sub ExporToExcel(strOpen As String)
    '*********************************************************
    '* 名称:ExporToExcel
    '* 功能:导出数据到EXCEL
    '* 用法:ExporToExcel(sql查询字符串)
    '*********************************************************
    Dim Rs_Data As New ADODB.Recordset
    Dim Irowcount As Integer
    Dim Icolcount As Integer
    Dim xlApp As New Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim xlQuery As Excel.QueryTable
    With Rs_Data
    If .State = adStateOpen Then
    .Close
    End If
    .ActiveConnection = Conn
    .CursorLocation = adUseClient
    .CursorType = adOpenStatic
    .LockType = adLockReadOnly
    .Source = strOpen
    .Open
    End With
    With Rs_Data
    If .RecordCount < 1 Then
        MsgBox ("No Date!")
        Exit Sub
    End If
    '记录总数
    Irowcount = .RecordCount
    '字段总数
    Icolcount = .Fields.Count
    End With
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = Nothing
    Set xlSheet = Nothing
    Set xlBook = xlApp.Workbooks().Add
    Set xlSheet = xlBook.Worksheets("sheet1")
    xlApp.Visible = True
    '添加查询语句,导入EXCEL数据
    Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))
    With xlQuery
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    '.Savepsw = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    End With
    xlQuery.FieldNames = True '显示字段名
    xlQuery.Refresh
    xlApp.Range("A:Z").NumberFormatLocal = "yyyy-M-d HH:mm:ss;@"
    xlApp.Application.Visible = True
    Set xlApp = Nothing '"交还控制给Excel
    Set xlBook = Nothing
    Set xlSheet = Nothing
    End Sub
      

  2.   

    不用保存到数据库,参见:
    http://community.csdn.net/Expert/TopicView3.asp?id=5603202
      

  3.   

    直接行列循环,读出数据,创建一个文本文件file.xls,写进去就可以了
      

  4.   

    对象能不调就不调,尤其excel对象在web里面调没啥好处,直接写文件速度很快,不会有副作用,呵呵