C # connect Excel 2003 and Excel 2007 and above as database connection string

Time:2022-5-16
Copy codeThe code is as follows:
string fileExt = Path.GetExtension(excelPath);
string conn = “”;
if (fileExt == “.xls”)
{
conn = “Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =” + excelPath + “;Extended Properties=’Excel 8.0;HDR=Yes;IMEX=1′”;
}
else
{
conn = “Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source =” + excelPath + “;Extended Properties=’Excel 12.0;HDR=Yes;IMEX=1′”;
}

03 is different from 07, 10 and other versions of the engine
03 is Microsoft Jet. OLEDB. four
07 and above are Microsoft ACE. OLEDB. twelve
And Microsoft ACE. OLEDB. 12.0 engine needs a file to download and install: http://www.microsoft.com/zh-cn/download/details.aspx?id=23734
In fact, the latter can be compatible with the former
Reference article link: http://blog.csdn.net/Limpire/article/details/2599760