使用 OleDbConnection 检索带有特殊字符的 Excel 工作表名称
嘿大家, 我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论