如何把datagrid和msflexgrid控件中的数据对应地写到excel或word中,不能通过后台的数据库表间接添入,要求就是把表中的内容实际给出到excel或word中

解决方案 »

  1.   

    如何把datagrid和msflexgrid控件中的数据对应地写到excel或word中,不能通过后台的数据库表间接添入,要求就是把表中的内容实际给出到excel或word中?尽量有原代吗,也可以把关键代码写上。
      

  2.   

    Dim Exc As Excel.Application
    Dim oWork As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    Dim vMergeCellstart As String
    Dim vMergeCellend As String
    Dim vList As String
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim l As Integer
    Dim x As Integer
    Dim vRow As Integer
    Dim vCol As Integer
    vList = "一二三四五六七八九十"
    Set Exc = New Excel.Application
    Set oWork = Exc.Workbooks.Add
    For i = 1 To TSS.Tabs.count
        If i > 3 Then
            Exc.Worksheets.Add
        End If
        Set oSheet = Exc.Worksheets(i)
        oSheet.Activate
        oSheet.Name = TSS.Tabs(i).Caption
        'oSheet.Range("A1", "A1").Select
        'oSheet.Range("A1", "A1").Font.Bold = True
        
           
        oSheet.Range("A1:G1").Select
        Exc.ActiveCell.FormulaR1C1 = "生产任务单" & "(" & TSS.Tabs(i).Caption & ")"
        With Exc.ActiveCell.Characters(Start:=1, Length:=5).Font
            .Name = "宋体"
            .FontStyle = "加粗"
            .Size = 14
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        '(道闸系统)变成10号字体
        With Exc.ActiveCell.Characters(Start:=6, Length:=6).Font
            .Name = "宋体"
            .FontStyle = "常规"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        '居中
        oSheet.Range("A1:G1").Select
        With Exc.Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        oSheet.Cells(2, 1) = "文件编号:QRCT0900" & i & "版次 1/1"
        oSheet.Range("A2:G2").Select
        Exc.ActiveCell.Font.Size = 8
            With Exc.Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        
        oSheet.Cells(3, 1) = "订货单位"
        oSheet.Cells(3, 3) = "工程名:"
        oSheet.Cells(3, 5) = "工单号:"
        vRow = 4
       
        For j = 1 To TS(i).Tabs.count
            vCol = 1
            oSheet.Cells(vRow, vCol) = Mid(vList, j, 1) & "、" & TS(i).Tabs(j).Caption
            vRow = vRow + 1
            vMergeCellstart = "A" & vRow
            For x = 1 To DG(vDG(i) + j - 1).Cols - 2                        '表头
                If x = 1 Then
                    oSheet.Cells(vRow, vCol) = "序号"
                Else
                    oSheet.Cells(vRow, vCol) = DG(vDG(i) + j - 1).TextMatrix(1, x)
                End If
                vCol = vCol + 1
            Next
           
            oSheet.Range("" & vMergeCellstart & ":G" & vRow & "").Select
            Exc.Selection.HorizontalAlignment = xlCenter
             vRow = vRow + 1
            For k = 2 To DG(vDG(i) + j - 1).Rows - 1                             '填写数据
                
                vCol = 1
                For l = 1 To DG(vDG(i) + j - 1).Cols - 2
                    If l = 1 Then
                        oSheet.Cells(vRow, vCol) = k - 1
                    Else
                        oSheet.Cells(vRow, vCol) = DG(vDG(i) + j - 1).TextMatrix(k, l)
                    End If
                    vCol = vCol + 1
                Next
                 vRow = vRow + 1
            Next
            vRow = vRow - 1
            '单位,数量字段居中。
            oSheet.Range("E" & Right(vMergeCellstart, Len(vMergeCellstart) - 1) & ":F" & vRow & "").Select
            Exc.Selection.HorizontalAlignment = xlCenter
            vMergeCellend = "G" & vRow
            '
        '画表格
        oSheet.Range("" & vMergeCellstart & ":" & vMergeCellend & "").Select
        Exc.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Exc.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Exc.Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Exc.Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Exc.Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Exc.Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Exc.Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Exc.Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
            vRow = vRow + 1
        NextNext
    Exc.Visible = True
    Exc.Worksheets(1).Activate
    Set Exc = Nothing
    Set oWork = Nothing
    Set oSheet = Nothing
    End Sub
      

  3.   

    谢谢你的帖子,我所需求的datagrid控件问题还是解决不了呀!不如下面一端程序,如何该一下,能读进去呢?
    Dim i
    Dim j
    For i = 0 To rst_thpublic.RecordCount - 1
    For j = 0 To dgtguestinfo.Columns.Count
    Wsheet.Cells(i + 1, j + 1) = dgtguestinfo.Columns(j)
    Next j
    rst_thpublic.MoveNext
    Me.dgtguestinfo.Refresh
    Next i
    rst_thpublic.MoveFirst
    这个程序中:  rst_thpublic变量应该怎么样定义呢? 我定义了一个记录集,还是出错的,请帮忙了?