C# 讀取Excel

網上一大堆拷貝粘貼的,不少過期了,本身驗證了一個,記錄下。ide

<pre><code> string strExcel = @"filename.xlsx"; string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcel + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; OleDbConnection dbConn = new OleDbConnection(strConn); try { dbConn.Open(); string strSql = "select * from [Master$]"; OleDbCommand myCommand = new OleDbCommand(strSql, dbConn); OleDbDataReader reader = myCommand.ExecuteReader(); if (reader == null) { return; } while(reader.Read()) { string strValue = Convert.ToString(reader.GetValue(0)); } } catch(Expection e) { } </code></pre>code

相關文章
相關標籤/搜索