使用 Excel OleDb 按工作表顺序获取工作表名称 [英] Using Excel OleDb to get sheet names IN SHEET ORDER

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

问题描述

我正在使用 OleDb 从包含多张工作表的 Excel 工作簿中读取数据.

I'm using OleDb to read from an excel workbook with many sheets.

我需要阅读工作表名称,但我需要按照它们在电子表格中定义的顺序;所以如果我有一个看起来像这样的文件;

I need to read the sheet names, but I need them in the order they are defined in the spreadsheet; so If I have a file that looks like this;

|_____|_____|____|____|____|____|____|____|____|
|_____|_____|____|____|____|____|____|____|____|
|_____|_____|____|____|____|____|____|____|____|
\__GERMANY__/\__UK__/\__IRELAND__/

那我需要拿字典

1="GERMANY", 
2="UK", 
3="IRELAND"

我尝试过使用 OleDbConnection.GetOleDbSchemaTable(),它提供了名称列表,但它按字母顺序对它们进行了排序.字母排序意味着我不知道特定名称对应于哪个工作表编号.所以我明白了;

I've tried using OleDbConnection.GetOleDbSchemaTable(), and that gives me the list of names, but it alphabetically sorts them. The alpha-sort means I don't know which sheet number a particular name corresponds to. So I get;

GERMANY, IRELAND, UK

改变了UKIRELAND的顺序.

我需要对它进行排序的原因是我必须让用户按名称或索引选择一个数据范围;他们可以要求从德国到爱尔兰的所有数据"或从表 1 到表 3 的数据".

The reason I need it to be sorted is that I have to let the user choose a range of data by name or index; they can ask for 'all the data from GERMANY to IRELAND' or 'data from sheet 1 to sheet 3'.

任何想法将不胜感激.

如果我可以使用办公室互操作类,这将很简单.不幸的是,我不能,因为互操作类在 Windows 服务和 ASP.NET 站点等非交互式环境中不能可靠地工作,所以我需要使用 OLEDB.

if I could use the office interop classes, this would be straightforward. Unfortunately, I can't because the interop classes don't work reliably in non-interactive environments such as windows services and ASP.NET sites, so I needed to use OLEDB.

推荐答案

在实际的 MSDN 文档中找不到这个,但论坛的版主说

Can't find this in actual MSDN documentation, but a moderator in the forums said

我担心 OLEDB 不会像在 Excel 中那样保留工作表顺序

I am afraid that OLEDB does not preserve the sheet order as they were in Excel

按工作表顺序排列的 Excel 工作表名称

似乎这是一个足够普遍的要求,有一个体面的解决方法.

Seems like this would be a common enough requirement that there would be a decent workaround.

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

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