• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C# 获取Excel工作薄中Sheet页(工作表)名集合

c# 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

C# 获取Excel工作薄中Sheet页(工作表)名集合

  #region 获取Excel工作薄中Sheet页(工作表)名集合        /// <summary>         /// 获取Excel工作薄中Sheet页(工作表)名集合        /// </summary>         /// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param>         /// <returns>Sheet页名称集合</returns>         private String[] GetExcelSheetNames(string fileName)        {            OleDbConnection objConn = null;            System.Data.DataTable dt = null;            try            {                string connString=string.Empty;                string FileType =fileName.Substring(fileName.LastIndexOf("."));                if (FileType == ".xls")                   connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +                    "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";                else//.xlsx                    connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";                  // 创建连接对象                 objConn = new OleDbConnection(connString);                // 打开数据库连接                 objConn.Open();                // 得到包含数据架构的数据表                 dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                if (dt == null)                {                    return null;                }                String[] excelSheets = new String[dt.Rows.Count];                int i = 0;                // 添加工作表名称到字符串数组                 foreach (DataRow row in dt.Rows)                {                    string strSheetTableName = row["TABLE_NAME"].ToString();                    //过滤无效SheetName                    if (strSheetTableName.Contains("$")&&strSheetTableName.Replace("'", "").EndsWith("$"))                    {                        excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1);                    }   <strong>本文来源gaodai#ma#com搞@@代~&码网</strong>                                    i++;                }                return excelSheets;            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());                return null;            }            finally            {                // 清理                 if (objConn != null)                {                    objConn.Close();                    objConn.Dispose();                }                if (dt != null)                {                    dt.Dispose();                }            }        }        #endregion

以上就是C# 获取Excel工作薄中Sheet页(工作表)名集合的内容,更多相关内容请关注搞代码(www.gaodaima.com)!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C# 获取Excel工作薄中Sheet页(工作表)名集合
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址