读取Excel和Access文件驱动有两种ACE和Jet

二者大同小异,ACE性能更好些,至于具体区别如下:

Jet 引擎,可以访问 Office 97-2003,但不能访问 Office 2007。
ACE 引擎是随 Office 2007 一起发布的数据库连接组件,既可以访问 Office 2007,也可以访问 Office 97-2003。
Microsoft.ACE.OLEDB.12.0 可以访问正在打开的 Excel 文件,而 Microsoft.Jet.OLEDB.4.0 是不可以的。

/// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="path"></param>
        public void SelectInfo(string path) {
            string Suffix = Path.GetExtension(path);
            //string tabName = "Student";
            string tabName = "";  //查询的表名 建议在窗体上面 加一个textbox控件
            if (tabName.Equals(""))
            {
                tabName += "[sheet1$]";
            }
            string strConn = string.Empty;
            //path = Environment.CurrentDirectory;
            switch (Suffix)
            {
                case ".xls":
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1;'";
                    //tabName += "$";
                    break;
                case ".xlsx":
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
                   // tabName += "$";
                    break;
                case ".mdb":
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
                    break;
                case ".accdb"://Access2007
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path;
                    break;
                default:
                    break;
            }
            OleDbConnection oledbConn = new OleDbConnection(strConn);
            try
            {
             //   oledbConn.Open();
                string sql = "Select * from " + tabName;
                OleDbDataAdapter oledbDa = new OleDbDataAdapter(sql, oledbConn);
                DataSet ds = new DataSet();
                oledbDa.Fill(ds,"信息");
                this.dgv_Info.DataSource = ds.Tables["信息"];
            }
            catch (Exception)
            {

                throw;
            }
            finally {
                oledbConn.Close();
            }
        }