从Excel获取工作表名称 [英] Getting Sheet Name From Excel

查看:64
本文介绍了从Excel获取工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将excel文件上传到SQL Server数据库.我目前正在使用此行从工作表中获取数据:

I am uploading excel files to a SQL Server database. I am currently using this line to get the data from the sheet:

string myQuery = "Select * from [Sheet1$]";

问题是,如果工作表名称不是 Sheet1 ,则它将失败.有没有一种方法可以获取工作表名称,而不是在 Sheet1 中进行硬编码?

The problem is, if the sheet name isn't Sheet1 then it will fail. Is there a way to get the sheet name rather than hard coding in Sheet1?

推荐答案

您可以先使用

You can query the schema first using GetOleDbSchemaTable:

DataTable schemaTable = connection.GetOleDbSchemaTable(
    OleDbSchemaGuid.Tables,
    new object[] { null, null, null, "TABLE" });

第一个表的名称应为 schemaTable.Rows [0] [0] .

这篇关于从Excel获取工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆