vb中如何把datagrid控件里的内容,另存为excel或是word文档形式。

解决方案 »

  1.   

    我只写过一段 LISTVIEW 到 XLS的代码  不过 在程序里重来没有用过Sub Listv2XLS(Listv As ListView)
    '用输出到excel的方法打印
    Dim Ex As New Excel.Application
    Dim ExW As Excel.Workbook
    Dim Exs As Excel.Worksheet
    Dim i As Integer
    Dim t As Integer
    Dim n As Integer
    On Error Resume Next
    Load Form_Wait
    Form_Wait.Show
    Form_Wait.Refresh
    Form_Wait.Maxid = Listv.ListItems.CountKill App.Path & "\intemp.xls"
    FileCopy App.Path & "\xls.dll", App.Path & "\intemp.xls"
    On Error GoTo 0
    Set Ex = CreateObject("Excel.Application")
    Set ExW = Ex.Workbooks.Open(App.Path & "\intemp.xls")
    Set Exs = ExW.Worksheets("sheet1")
    With Exs
    '增加表头
        For i = 1 To Listv.ColumnHeaders.Count
            .Cells(2, i).Font.Name = "Times New Roman"
            .Cells(2, i).Font.Size = 10
            .Cells(2, i).Font.Italic = False
            .Cells(2, i) = Listv.ColumnHeaders(i).Text
        
        Next
        
        For i = 1 To Listv.ListItems.Count
        Form_Wait.aa = i
            For t = 1 To Listv.ColumnHeaders.Count
                .Cells(2 + i, t).Font.Name = "Times New Roman"
                .Cells(2 + i, t).Font.Size = 10
                .Cells(2 + i, t).Font.Italic = True
                If t = 1 Then
                    .Cells(2 + i, t) = Listv.ListItems(i).Text
                Else
                    .Cells(2 + i, t) = Listv.ListItems(i).SubItems(t - 1)
                End If
            Next
        Next
    End With Unload Form_Wait
    Exs.Application.Visible = TrueEnd Sub
      

  2.   

    直接保存是行不通的,必须输出到EXCEL中,方法就是grid1.TextMatrix(i,j)--> cell(i,j),再EXS.SAVE