2020年5月9日 星期六

vb.net ListView 加到 Excel [Add ListVIew item to Microsoft Excel]

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

--------------------------------存成DOCX--------------------------------

  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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
Imports Microsoft.Office.Interop

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        Dim objApp As Word.Application

        Dim objDoc As Word.Document

        objApp = New Word.Application()

        objDoc = objApp.Documents.Add

        objDoc.Activate()

        objDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperLetter

        objDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape

        objDoc.PageSetup.TopMargin = 30

        objDoc.PageSetup.BottomMargin = 30

        objDoc.PageSetup.LeftMargin = 30

        objDoc.PageSetup.RightMargin =


        Dim rng As Word.Range = objDoc.Range(0, 0)

        rng.InsertBefore(Me.Text)

        rng.Font.Size = 20

        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter

        rng.Font.Bold = True

        rng.InsertParagraphAfter()

        rng.InsertParagraphAfter()

        rng.SetRange(rng.End, rng.End)


        objDoc.Tables.Add(Range:=rng, NumRows:=ListView1.Items.Count + 1, NumColumns:=ListView1.Columns.Count)


        Dim tbl As Word.Table = objDoc.Tables(1)


        Dim rngCell As Word.Range

        For i As Integer = 1 To ListView1.Columns.Count

            rngCell = tbl.Cell(1, i).Range

            rngCell.Text = ListView1.Columns.Item(i - 1).Text

            rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter

            rngCell.Shading.ForegroundPatternColor = Word.WdColor.wdColorGray15

            rngCell.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle

            rngCell.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle

        Next


        For i As Integer = 1 To ListView1.Items.Count

            For j As Integer = 1 To ListView1.Columns.Count

                rngCell = tbl.Cell(i + 1, j).Range

                rngCell.Text = ListView1.Items.Item(i - 1).SubItems(j - 1).Text

                rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft

                rngCell.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle

                rngCell.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle

            Next j

        Next i


        For Each section As Word.Section In objDoc.Sections

            Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

            footerRange.Text = "-" & Me.Text & "- generado por ""Corporation Inventory Management"""

        Next


        objDoc.SaveAs("C:\PDFs\OK.docx")

        objDoc.Close()

        objApp.Quit()

        objDoc = Nothing

        objApp = Nothing


    End Sub

沒有留言:

張貼留言