oledb connection string for Excel 2016 in C#

我的未来我决定 提交于 2019-12-14 03:39:48

问题


I have been trying to access 2016 MS Excel file using C#, but connection string is working only till 2013 MS Excel.

My current connection string:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx; Extended Properties="Excel 12.0 Xml;HDR=YES";

Is there any modified oledb connection string for MS Excel 2016?


回答1:


This occurred for me after upgrading from a local install of Office 13 to Office 16 through the Office 365 program. I was getting this exception: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

I was not able to find a way to install the driver through the office 365 install process.

I had to install https://www.microsoft.com/en-us/download/details.aspx?id=13255 - the x64 version did not solve the issue, had to use the 32bit version.

My connection string in App.config

    <add key="Excel07ConnectionString" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>

Code using it:

            var excelConnectionString = ConfigurationSettings.GetExcelConnection(fileLocation);
            var dataTable = new DataTable();

            using (var excelConnection = new OleDbConnection(excelConnectionString))
            {
                excelConnection.Open();
                var dataAdapter = new OleDbDataAdapter("SELECT * FROM [Users$]", excelConnection);
                dataAdapter.Fill(dataTable);
                excelConnection.Close();
            }
            Console.WriteLine("OpenExcelFile: File successfully opened:" + fileLocation);
            return dataTable;



回答2:


This worked for me:

private string ExcelConnection(string fileName)
{
    return @"Provider=Microsoft.Jet.OLEDB.4.0;" +
           @"Data Source=" + fileName + ";" +
           @"Extended Properties=" + Convert.ToChar(34).ToString() +
           @"Excel 8.0" + Convert.ToChar(34).ToString() + ";";
}


来源:https://stackoverflow.com/questions/37195033/oledb-connection-string-for-excel-2016-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!