1. 程式人生 > >.net讀取excel

.net讀取excel

 private void button1_Click(object sender, EventArgs e)
        {
            string xlsPath = "F:/exercise/test.xls"; // 絕對物理路徑
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Extended Properties=/"Excel 8.0;HDR=No;IMEX=1/";" +
                            "data source=" + xlsPath;
            // 查詢語句
            string sql = "SELECT * FROM [Sheet1$]";
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
            da.Fill(ds);    // 填充DataSet       

            // 在這裡對DataSet中的資料進行操作       

            // 輸出,繫結資料
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
          


        }