C#中Excel 2016的oledb连接字符串

Rav*_*and 6 c# connection excel vba

我一直在尝试使用C#访问2016 MS Excel文件,但连接字符串仅工作到2013 MS Excel.

我当前的连接字符串:

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:\ myFolder\myExcel2007file.xlsx; 扩展属性="Excel 12.0 Xml; HDR = YES";

MS Excel 2016是否有任何修改过的oledb连接字符串?

JnJ*_*Boo 10

通过Office 365程序从本地安装的Office 13升级到Office 16后,我发生了这种情况.我得到了这个例外:'Microsoft.ACE.OLEDB.12.0'提供程序未在本地计算机上注册.

我无法通过Office 365安装过程找到安装驱动程序的方法.

我不得不安装https://www.microsoft.com/en-us/download/details.aspx?id=13255 - x64版本没有解决问题,不得不使用32位版本.

我在App.config中的连接字符串

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

使用它的代码:

            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;
Run Code Online (Sandbox Code Playgroud)


小智 5

这对我有用:

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() + ";";
}
Run Code Online (Sandbox Code Playgroud)