Re: [問題]C# 讀寫excel file

看板C_Sharp作者 (imio)時間17年前 (2009/03/08 14:44), 編輯推噓2(202)
留言4則, 2人參與, 最新討論串4/6 (看更多)
感謝感謝..可是會有一個問題為什麼first row會讀不到阿...而且不能讀csv fileㄟ只能讀 .xls file.麻煩各位高手在幫幫小弟....謝謝 ※ 引述《rookie (rookie)》之銘言: : 我只會讀的部份,就參考一下唄~~ : 其他請各位高手指導一下,謝謝! : using System.Data.SqlClient; : using System.Data.OleDb; : private void btnLoad_Click(object sender, EventArgs e) : { : string cnStr = "Data Source=C:\\Database\\employee.xls;Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;"; : using (OleDbConnection cn = new OleDbConnection(cnStr)) : { : using (OleDbCommand cmd = new OleDbCommand()) : { : cmd.Connection = cn; : cn.Open(); : cmd.CommandText = "SELECT * from [data$]"; //sheet name : using (OleDbDataReader dr = cmd.ExecuteReader()) : { : while (dr.Read()) : { : ListBox1.Items.Add(dr[0].ToString() + "\t" + dr[1].ToString() + "\t" + dr[2].ToString()); : } : } : } : } : } : ※ 引述《DearKurt ("小朋友"會自己找出路...)》之銘言: : : 如果執行程式時 excel檔是關閉的 : : 這樣的情況下 可以把excel檔當作資料庫去操作 : : 連線方式可以去connectionstring網站找 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 24.191.101.238

03/08 18:30, , 1F
CSV用streamreader就可以讀了吧?
03/08 18:30, 1F

03/08 18:30, , 2F
呼叫OLE元件還浪費系統資源
03/08 18:30, 2F

03/08 20:10, , 3F
可是用OLE讀取值方便很多耶…
03/08 20:10, 3F

03/08 23:25, , 4F
那掃一次作成List<>呢?
03/08 23:25, 4F
文章代碼(AID): #19isbIhA (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #19isbIhA (C_Sharp)