使用 OleDbConnection 检索带有特殊字符的 Excel 工作表名称

发布于 2024-11-02 14:21:42 字数 744 浏览 4 评论 0原文

嘿大家, 我正在尝试使用 OleDbConnection 从 Excel 文件中获取工作表名称列表。 (注意:该程序将在服务器上运行,因此无法选择使用 Office 互操作类)。因此,使用以下代码一切正常,并返回工作表名称:

        var dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        foreach (DataRow row in dt.Rows)
        {
            string sheetName = row["TABLE_NAME"].ToString();
        }

但是,如果 Excel 工作表名称包含点“.”,则会转换为“#”。例如,如果 Excel 文件具有以下工作表名称:

Sheet.1
Sheet#1

上面的代码将返回:

"Sheet#1$"
"'Sheet#1$'"

并且工作表名称现在基本相同。变得更糟!当按照以下代码从 Sheet.1 读取数据时:

var adapter = new OleDbDataAdapter("SELECT * FROM ['Sheet.1$']", connectionString);

它实际上会从“sheet#1”而不是“sheet.1”读取数据。

任何帮助将不胜感激。

Hey all,
I am trying to get a list of sheet names from an Excel file using OleDbConnection. (NOTE: This program will run on a server so no option to use office interop classes). So everything works fine with the following code and it bringing back the sheet names:

        var dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        foreach (DataRow row in dt.Rows)
        {
            string sheetName = row["TABLE_NAME"].ToString();
        }

However if the excel sheet name contains a dot “.”, that is converted in a “#”. For example if the excel file has the following sheet names:

Sheet.1
Sheet#1

The code above will return:

"Sheet#1$"
"'Sheet#1

And the sheet names are now basically the same. Gets worse!!! when reading data from Sheet.1 as per the following code:

var adapter = new OleDbDataAdapter("SELECT * FROM ['Sheet.1

it will actually read data from "sheet#1" NOT "sheet.1".

Any help will be appreciated.

"

And the sheet names are now basically the same. Gets worse!!! when reading data from Sheet.1 as per the following code:


it will actually read data from "sheet#1" NOT "sheet.1".

Any help will be appreciated.

]", connectionString);

it will actually read data from "sheet#1" NOT "sheet.1".

Any help will be appreciated.

"

And the sheet names are now basically the same. Gets worse!!! when reading data from Sheet.1 as per the following code:

it will actually read data from "sheet#1" NOT "sheet.1".

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文