关于Excel导入(或数据库连接)的HDR=YES; IMEX=1详解

 #region xml驱动----------------------------
        /// <summary>
        /// 唯一需要注意的是,如果目标机器的操作系统,是64位的话。
        /// 项目需要 编译为 x86,而不是简单的使用默认的 Any CPU.
        /// </summary>
        /// <param name="strExcelFileName"></param>
        /// <returns></returns>
        private string GetOleDbConnectionString(string strExcelFileName)
        {
            // Office 2007 以及 以下版本使用.
            string strJETConnString =
              String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'", strExcelFileName);
            // xlsx 扩展名 使用.xlsx
            string strASEConnXlsxString =
              String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=NO;IMEX=1;\"", strExcelFileName);
            // xls 扩展名 使用.
            string strACEConnXlsString =
              String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO\"", strExcelFileName);
            //其他
            string strOtherConnXlsString =
              String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'", strExcelFileName);

            //cp
            string cp_strACEConnXlsString =
              String.Format("Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=NO; IMEX=1'", strExcelFileName);

            //cp2
            string cp_strACEConnXlsString_jet12 =
              String.Format("Provider=Microsoft.Jet.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=NO; IMEX=1'", strExcelFileName);


            //1.[cp2]尝试使用 cp写的ACE. 假如不发生错误的话,使用 ACE 驱动.
            try
            {
                System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(cp_strACEConnXlsString_jet12);
                cn.Open();
                cn.Close();
                // 使用 ACE
                return cp_strACEConnXlsString;
            }
            catch (Exception ex)
            {
                // 启动 ACE 失败. 
                string msg = ex.Message;
            }


            //2.尝试使用 cp写的ACE. 假如不发生错误的话,使用 ACE 驱动.
            try
            {
                System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(cp_strACEConnXlsString);
                cn.Open();
                cn.Close();
                // 使用 ACE
                return cp_strACEConnXlsString;
            }
            catch (Exception ex)
            {
                // 启动 ACE 失败. 
                string msg = ex.Message;
            }



            //3.尝试使用 ACE. 假如不发生错误的话,使用 ACE 驱动.
            try
            {
                System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(strACEConnXlsString);
                cn.Open();
                cn.Close();
                // 使用 ACE
                return strACEConnXlsString;
            }
            catch (Exception ex)
            {
                // 启动 ACE 失败.
                string msg = ex.Message;
            }

            // 4.尝试使用 Jet. 假如不发生错误的话,使用 Jet 驱动.
            try
            {
                System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(strJETConnString);
                cn.Open();
                cn.Close();
                // 使用 Jet
                return strJETConnString;
            }
            catch (Exception ex)
            {
                // 启动 Jet 失败.
                string msg = ex.Message;
            }

            // 5.尝试使用 Jet. 假如不发生错误的话,使用 Jet 驱动.
            try
            {
                System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(strASEConnXlsxString);
                cn.Open();
                cn.Close();
                // 使用 Jet
                return strASEConnXlsxString;
            }
            catch (Exception ex)
            {
                // 启动 Jet 失败.
                string msg = ex.Message;
            }
            // 假如 ACE 与 JET 都失败了,默认使用 JET.
            return strOtherConnXlsString;
        }

        #endregion xml驱动----------------------------
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'

参数HDR的值:
HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
参数Excel 8.0
对于Excel 97以上版本都用Excel 8.0
IMEX ( IMport EXport mode )设置
  IMEX 有三种模式:
  0 is Export mode
  1 is Import mode
  2 is Linked mode (full update capabilities)
  我这里特别要说明的就是 IMEX 参数了,因为不同的模式代表著不同的读写行为:
  当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。
  当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。
  当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”用途。
意义如下:
0 ---输出模式;
1---输入模式;
2----链接模式(完全更新能力)

发布了252 篇原创文章 · 获赞 94 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/cplvfx/article/details/103237296