C#获取Excel sheet页签信息

/// <summary>
        /// 获取Excel下sheet页名称
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public Dictionary<int, string> ExcelSheetName(string filepath)
        {
            Dictionary<int, string> names = new Dictionary<int, string>();
            string strConn;
            strConn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=2'";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataTable sheetNames = conn.GetOleDbSchemaTable
            (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            conn.Close();
            for (int i = 0; i < sheetNames.Rows.Count; i++)
            {
                names.Add(i, sheetNames.Rows[i]["TABLE_NAME"].ToString());
            }
            //下拉框赋值
            //BindingSource bs = new BindingSource();
            //bs.DataSource = names;
            //sheetList.ValueMember = "key";
            //sheetList.DisplayMember = "value";
            //this.sheetList.DataSource = bs;

            return names;
        }

猜你喜欢

转载自blog.csdn.net/CONSOLE11/article/details/119735682