C#自动判断Excel版本使用不同的连接字符串
用OLEDB通过设置连接字符串可以像读取sqlserver一样将excel中的数据读取出来,但是excel2003和excel2007/2010的连接字符串是不同的。
/// <summary> /// 把数据从Excel装载到DataTable /// </summary> /// <param name="pathName">带路径的Excel文件名</param> /// <param name="sheetName">工作表名</param> /// <param name="tbContainer">将数据存入的DataTable</param> /// <returns></returns> public DataTable ExcelToDataTable(string pathName, string sheetName) { DataTable tbContainer = new DataTable(); string strConn = string.Empty; if (string.IsNullOrEmpty(sheetName)) { sheetName = "Sheet1"; } FileInfo file = new FileInfo(pathName); if (!file.Exists) { throw new Exception("文件不存在"); } string extension = file.Extension; switch (extension) { case ".xls": strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; break; case ".xlsx": strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; break; default: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; break; } //链接Excel OleDbConnection cnnxls = new OleDbConnection(strConn); //读取Excel里面有 表Sheet1 OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), cnnxls); DataSet ds = new DataSet(); //将Excel里面有表内容装载到内存表中! oda.Fill(tbContainer); return tbContainer; }
这里需要注意的地方是,当文件的后缀名为.xlsx(excel2007/2010)时的连接字符串是"Provider=Microsoft.ACE.OLEDB.12.0;....",注意中间红色部分不是"Jet"。
赞 (0)