2020年5月9日 星期六

vb.net DataGridView 加到 Word [Add DataGridView to Microsoft Word]

參考裡加入 Microsoft.Office.Interop.Word.Application()

--------------------------------直接顯示--------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim objWord As Word.Application

        Dim objDoc As Word.Document


        objWord = CreateObject("Word.Application")

        objWord.Visible = True

        objDoc = objWord.Documents.Add


        Dim _RowCount As Integer = DataGridView1.Rows.Count - 1

        Dim _ColCount As Integer = DataGridView1.Columns.Count - 1


        Dim ht1 As Word.Table


        ht1 = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, _

                                _RowCount + 1, _ColCount + 1)

        ht1.Borders.OutsideColor = Word.WdColor.wdColorBlack

        ht1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle

        ht1.Borders.InsideColor = Word.WdColor.wdColorBlack

        ht1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle


        For i As Integer = 0 To _RowCount

            ht1.Rows.Add()

            For _col As Integer = 0 To _ColCount

                Dim colType As Type = DataGridView1.Columns(_col).GetType

                If colType.Name = "DataGridViewImageColumn" Then

                    Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).Value, Image)

                    Clipboard.SetImage(_image)

                    ht1.Cell(i + 1, _col + 1).Range.Paste()

                Else

                    ht1.Cell(i + 1, _col + 1).Range.Text = _

                    DataGridView1.Rows(i).Cells(_col).Value.ToString()

                End If

            Next

        Next

        objDoc.SaveAs2("C:/tee.docx")

    End Sub


Imports Word = Microsoft.Office.Interop.Word


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        ' Create Word Application


        Dim oWord As Word.Application = CreateObject("Word.Application")


        ' Create new word document


        Dim oDoc As Word.Document = oWord.Documents.Add()


        oWord.Visible = True




        'Insert a 3 x 5 table and fill it with specific data


        Dim r As Integer, c AsInteger


        Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)


        oTable.Range.ParagraphFormat.SpaceAfter = 6


        For r = 1 To 3


            For c = 1 To 5


                oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c


            Next


        Next


        'make the first row bold and italic


        oTable.Rows.Item(1).Range.Font.Bold = True


        oTable.Rows.Item(1).Range.Font.Italic = True


        ' Save this word document


        oDoc.SaveAs("C:\myfile.doc", True)


        oDoc.Close()


        oWord.Application.Quit()


    EndSub

沒有留言:

張貼留言