2020年5月25日 星期一

vb.net 使用 OleDb 讀取 Excel 內容並寫入 ListView [Use OleDb read Excel to Listview]


1
2
3
4
5
6
Imports Microsoft.Office.Interop
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb





 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
    Private Sub ReadXLSX(strPath As String)
        Dim conn As OleDb.OleDbConnection
        Dim dta As OleDbDataAdapter
        Dim dts As DataSet

        conn = New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=Excel 12.0;")
        dta = New OleDbDataAdapter("select * from [各壇報名表$]", conn)
        dts = New DataSet
        dta.Fill(dts, "[各壇報名表$]")
        dtt = dts.Tables("[各壇報名表$]")
        conn.Close()

        For Each row As DataRow In dtt.Rows()
            If row(0).ToString = "完成" Then
                Dim lvwi As ListViewItem
                lvwi = ListView1.Items.Add(row(1).ToString)
                lvwi.SubItems.Add(row(2).ToString)
                lvwi.SubItems.Add(row(3).ToString)
                lvwi.SubItems.Add(row(4).ToString)
                lvwi.SubItems.Add(row(5).ToString)
                lvwi.SubItems.Add(row(6).ToString)
                lvwi.SubItems.Add(row(7).ToString)
                lvwi.SubItems.Add(row(8).ToString)
                lvwi.SubItems.Add(row(9).ToString)
                lvwi.SubItems.Add(row(10).ToString)
                lvwi.SubItems.Add(row(11).ToString)
                lvwi.SubItems.Add(row(12).ToString)
                lvwi.SubItems.Add(row(13).ToString)
            End If
        Next

    End Sub

沒有留言:

張貼留言