[英]oledb connection string for Excel 2016 in C#
我一直在尝试使用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连接字符串?
通过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'"/>
使用它的代码:
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;
这对我有用:
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() + ";";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.