public string BuildExcelConnectionString(string filename) {
string ext = System.IO.Path.GetExtension(filename);
// Support for Excel 2007 XML format
if (ext.ToLower() == ".xlsx")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", filename);
// Support for Excel 2007 binary format
if (ext.ToLower() == ".xlsb")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES\";", filename);
// Support for Excel 2007 binary format
if (ext.ToLower() == ".xlsm")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=YES\";", filename);
// Basic support for Excel 97 through 2003.
return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES\";", filename);
}
string ext = System.IO.Path.GetExtension(filename);
// Support for Excel 2007 XML format
if (ext.ToLower() == ".xlsx")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", filename);
// Support for Excel 2007 binary format
if (ext.ToLower() == ".xlsb")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES\";", filename);
// Support for Excel 2007 binary format
if (ext.ToLower() == ".xlsm")
return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=YES\";", filename);
// Basic support for Excel 97 through 2003.
return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES\";", filename);
}
本文介绍如何根据Excel文件类型构建相应的连接字符串,支持从Excel 97到2007的不同格式。
1万+

被折叠的 条评论
为什么被折叠?



