[C#] - 如何使用Oledb計算Excel Count

摘要:[C#] - 如何使用Oledb計算Excel Count

使用oledb去對excel檔案做處理,

想要計算總共有幾筆資料,

問題的解決方式如下:


string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; 
Data Source =Data.xls;Extended Properties='Excel 8.0;HDR=YES'";
//連結字串中的HDR=YES,代表略過第一欄資料
OleDbConnection oledb_con = new OleDbConnection(strCon);
oledb_con.Open();
OleDbCommand oledb_com = new OleDbCommand(" SELECT COUNT(*) AS EXP1 FROM [sheet1$] ", oledb_con);
OleDbDataReader oledb_dr = oledb_com.ExecuteReader();
oledb_dr.Read();
MessageBox.Show(oledb_dr["exp1"].ToString());

如果有連結excel的問題可以參考這篇"讀取excel檔案"

相關參考:

http://www.bygsoftware.com/Excel/SQL/UsingSql.html

http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm

↑尋找資料意外找到的好東西!!

P.S.查完資料的感想是使用oledb excel sql敘述式的相關資料有夠難找!替代方案是找MS-SQL的敘述式,不過要看一下相關參考的網址,有些指令是不適用的