dim i,j as integer
for i=0 to mshflexgrid1.rows-1
    for j=0 to mshflexgrid1.cols-1
        xlsheet.cells(i+1,j+1).value=mshflexgrid1.textmatrix(i,j)
    next
next
xlsheet.printout

解决方案 »

  1.   

    '将数据输出为excel文件
    Private Sub s_OutToExcel()
    Screen.MousePointer = vbHourglass
    Dim oXl As Excel.Application
    Dim oWb As Workbook
    Dim oWs As Excel.Worksheet
    Dim iA, iB, iC, iD
    Dim bExcelRunning    'Excel是否已运行
    Dim flg As MSFlexGrid
    Dim sStr
    On Error GoTo Morn
    bExcelRunning = True     '同首先用的GetObject一致:假设Excel已运行
    Set oXl = GetObject("", "Excel.Application")
    Set oWb = oXl.Workbooks.Add
    Set oWs = oWb.Worksheets(1)
    Set flg = mOform.flg
    With oWs
        For iB = 1 To flg.Cols
          .Cells(1, iB).Value = flg.TextMatrix(0, iB - 1)
        Next
        For iC = 1 To flg.Rows - 1
            For iB = 1 To flg.Cols
             sStr = Trim(flg.TextMatrix(iC, iB - 1))
             If IsNumeric(sStr) Then
                .Cells(iC + 1, iB).Value = sStr
             Else
                '截取尾部的数字
                If IsNumeric(Right(sStr, 1)) Then sStr = Left(sStr, Len(sStr) - 1)
                If IsNumeric(Right(sStr, 1)) Then sStr = Left(sStr, Len(sStr) - 1)
                .Cells(iC + 1, iB).Value = sStr
             End If
             
            Next
        Next
    End With
    oWs.Parent.Names.Add "CostRange", "=" & "A1:B39"
    sStr = App.Path & "\" & mOform.Caption & ".xls"
    oWs.SaveAs sStr
    Screen.MousePointer = vbDefault
    If MsgBox("已将数据输出到Excel文件中! 现在打开该文件?", vbQuestion + vbYesNo, "已完成") = vbNo Then
       oXl.Quit
    Else
       oXl.Visible = True
    End If
    Set oXl = Nothing
    Set oWs = Nothing
    Set oWb = NothingExit Sub
    Morn:
    Select Case Err.number
       Case 429
         Set oXl = GetObject("", "Excel.Application")
         bExcelRunning = False
         Resume Next
       Case 1004
         Screen.MousePointer = 0
         iA = MsgBox("发生了错误" & Err.number & ": " & Err.Description, vbExclamation + vbAbortRetryIgnore + vbDefaultButton3, "错误")
         If iA = vbAbort Then Exit Sub
         If iA = vbRetry Then
           Resume
         Else
           Resume Next
         End If
       Case Else
            MornSubs.sub_ErrCenter False
    End Select
    End Sub
      

  2.   

    Dim ex As Object
                    Dim exwbook As Object
                    Dim exsheet As Object
                    Dim Rs As Recordset
                    Dim FilePath As String
                    Dim MyVal As Integer
                    Set ex = CreateObject("Excel.Application")
                    Set exwbook = Nothing
                    Set exsheet = Nothing
                    Set exwbook = ex.Workbooks().Add
                    Set exsheet = exwbook.Worksheets("sheet1")
                    For j = 0 To mshflexgrid.Rows - 1
                        For i = 1 To mshflexgrid.Cols
                            ex.Range(Chr(96 + i) & CStr(j + 1)) =mshflexgrid.TextMatrix(j, i - 1)
                        Next
                    Next
                     FilePath = .FileName
                     exwbook.SaveAs FilePath
                      '退出excel
                     ex.Quit
                    Set exwbook = Nothing
                    Set exsheet = Nothing
                    Set ex = Nothing