2020年5月17日 星期日

vb.net ListView 加到 Excel [Listview export to Excel]

References 裡加入 Microsoft.Office.Interop.Excel.Application()

References->Add->Assemblies->Extensions-> Check Microsoft.Office.Interop.Excel


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

 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
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim MyExcel As New Microsoft.Office.Interop.Excel.Application()

        MyExcel.Application.Workbooks.Add()

        MyExcel.Visible = True


        '獲取標題

        Dim Cols As Integer

        For Cols = 1 To ListView1.Columns.Count

            MyExcel.Cells(1, Cols) = ListView1.Columns(Cols - 1).Text '.Columns(Cols - 1).HeaderText

        Next


        '往excel表裡添加資料()

        Dim i As Integer

        For i = 0 To ListView1.Items.Count - 1

            Dim j As Integer

            For j = 0 To ListView1.Columns.Count - 1

                MyExcel.Cells(i + 2, j + 1) = ListView1.Items.Item(i).SubItems(j).Text


            Next (j)

        Next (i)

    End Sub


--------------------------------存成XLSX--------------------------------

  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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
Imports Microsoft.Office.Interop


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim xls As New Excel.Application

        Dim book As Excel.Workbook

        Dim sheet As Excel.Worksheet

        'create a workbook and get reference to first worksheet

        xls.Workbooks.Add()


        book = xls.ActiveWorkbook

        sheet = book.ActiveSheet

        'step through rows and columns and copy data to worksheet

        Dim row As Integer = 1

        Dim col As Integer = 1


        sheet.Range(sheet.Cells(1, 1), sheet.Cells(1, ListView1.Columns.Count)).Merge()

        sheet.Cells(row, col) = Me.Text

        sheet.Cells(row, col).HorizontalAlignment = Excel.Constants.xlCenter

        sheet.Cells(row, col).Font.Size = 20


        row += 2

        col = 1


    sheet.Columns("A:B").ColumnWidth = 20 'AutoFit必需關閉


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

            sheet.Cells(row, col) = ListView1.Columns.Item(i - 1).Text

            sheet.Cells(row, col).HorizontalAlignment = Excel.Constants.xlCenter

            'sheet.Cells(row, col).Font.Name = "Arial"

            'sheet.Cells(row, col).Font.Bold = True

            'sheet.Cells(row, col).Font.Size = 12

            'sheet.Cells(row, col).Font.ColorIndex = 35

            sheet.Cells(row, col).EntireColumn.AutoFit()

            sheet.Cells(row, col).Interior.ColorIndex = 15

            col = col + 1

        Next

        row += 1

        col = 1


        For Each item As ListViewItem In ListView1.Items

            For i As Integer = 0 To item.SubItems.Count - 1

                sheet.Cells(row, col) = item.SubItems(i).Text

                sheet.Cells(row, col).EntireColumn.AutoFit()

                col = col + 1

            Next

            row += 1

            col = 1

        Next



        With sheet.Range(sheet.Cells(3, 1), sheet.Cells(ListView1.Items.Count + 3, ListView1.Columns.Count))

            With .Borders(Excel.XlBordersIndex.xlEdgeLeft)

                .LineStyle = Excel.XlLineStyle.xlDouble

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

            With .Borders(Excel.XlBordersIndex.xlEdgeTop)

                .LineStyle = Excel.XlLineStyle.xlContinuous

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

            With .Borders(Excel.XlBordersIndex.xlEdgeBottom)

                .LineStyle = Excel.XlLineStyle.xlContinuous

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

            With .Borders(Excel.XlBordersIndex.xlEdgeRight)

                .LineStyle = Excel.XlLineStyle.xlContinuous

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

            With .Borders(Excel.XlBordersIndex.xlInsideVertical)

                .LineStyle = Excel.XlLineStyle.xlContinuous

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

            With .Borders(Excel.XlBordersIndex.xlInsideHorizontal)

                .LineStyle = Excel.XlLineStyle.xlContinuous

                .ColorIndex = 0

                .TintAndShade = 0

                .Weight = Excel.XlBorderWeight.xlThin

            End With

        End With


        'save the workbook and clean up

        sheet.PageSetup.LeftFooter = "-" & Me.Text & "- generado por ""Corporation Inventory Management"""

        book.SaveAs("C:\PDFs\Test.xlsx")

        xls.Workbooks.Close()

        xls.Quit()

        releaseObject(sheet)

        releaseObject(book)

        releaseObject(xls)

    End Sub


    Private Sub releaseObject(ByVal obj As Object)

        'Release an automation object

        Try

            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)

            obj = Nothing

        Catch ex As Exception

            obj = Nothing

        Finally

            GC.Collect()

        End Try

    End Sub


沒有留言:

張貼留言