在查询Excel 2010时诊断OLEDBexception
要通过SQL查询Excel表格,我曾经使用过:
Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=""Excel 8.0;IMEX=1;HDR=YES;"""
要么
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + strPath + ";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""
现在这个工作正常,直到我安装Office 2010。
现在我得到一个
Microsoft.Ace.OLEDB.12.0提供程序未在此计算机上注册exception。
我怎样才能find正确的连接string/提供者?
也许你卸载了Access数据库引擎(ACE)组件? 它们仍然可以从MSDN下载,如2007 Office System Driver:Data Connectivity Components 。
我相信对于Excel 2010,它是:
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=D:\\MyDocs\\oledb.xlsx;Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:Engine Type=37"
这似乎在我的视觉工作室工作,我得到Excel生成查询string,它有额外的条目。
我下载并安装了上述的Office系统驱动程序:数据连接组件,下面的代码工作:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Password=\"\";User ID=Admin;Data Source=d:\\Sample.xlsx;Mode=Share Deny Write;Extended Properties=\"HDR=YES;\";Jet OLEDB:Engine Type=37"; OleDbConnection connection = new OleDbConnection(connectionString); try { connection.Open(); OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = command; DataSet ds = new DataSet(); adapter.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } catch (Exception) { //throw; } finally { connection.Close(); }