以下就來看code說故事
DataSet myDataset = new DataSet(); //OleDbConnection myOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "d:\\TKK.xls" + ";Extended Properties=Excel 8.0;");
//上面這行舊版寫法,只能匯入2003已下版本(包含) // OleDbConnection myOleDbConnection = new OleDbConnection("Provider=Microsoft.ace.oledb.12.0;Data Source=" + "d:\\TKK_Bak.xlsx" + ";Extended Properties=Excel 12.0;"); OleDbConnection myOleDbConnection = new OleDbConnection("Provider=Microsoft.ace.oledb.12.0;Data Source=" + "d:\\TKK.xls" + ";Extended Properties=Excel 12.0;"); OleDbCommand myOleDbCommand = new OleDbCommand("SELECT ZZZ,KKK,BBB,CCC FROM [Sheet1$]", myOleDbConnection);
// [Sheet1$] 表示搜尋Excel的第一個Sheet裡面的內容反之 [Sheet2]就是第二個 OleDbDataAdapter myData = new OleDbDataAdapter(myOleDbCommand); myData.Fill(myDataset); DataTable dt_tbl = myDataset.Tables[0]; for (int i = 0; i < dt_tbl.Rows.Count; i++) { Response.Write(dt_tbl.Rows[i][1].ToString()); }
// 淺顯易懂 XD
全站熱搜
留言列表