2011年5月18日 星期三

VB 2008 Excel 連線

1. 主動台模式
2. Execl 檔案 d:\test1.xls
3. 第一列為欄位名稱,請用文字(否則會被更改)
4. 程式碼:
Imports System.Data
Imports System.Data.OleDb
Module Module1
    Sub Main()
        'OleDbDataAdapter cmd;
        Dim cmd As OleDbDataAdapter
        Dim ds As DataSet = New DataSet()
        'String fileNameString = this.MapPath();
        'fileNameString += "\\" + fileNameTextBox.Text;
        Dim cn As OleDbConnection = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" & "D:\test1.xls" & ";Extended Properties=Excel 8.0;")
        'Select the data from Sheet1 of the workbook.
        Dim SQL As String = "select * from [工作表1$]"
        cmd = New OleDbDataAdapter(SQL, cn)
        cn.Open()
        cmd.Fill(ds, "test0")
        cn.Close()
        Console.WriteLine(ds.Tables(0).Columns(0).Caption)
        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            Console.WriteLine(ds.Tables(0).Rows(i).ItemArray(0))
        Next
    End Sub
End Module

沒有留言:

張貼留言