使用GetOleDbSchemaTable读取时Excel文件中的工作表顺序

本文关键字:工作 顺序 文件 GetOleDbSchemaTable 读取 Excel 使用 | 更新日期: 2023-09-27 18:25:17

我正在使用下面的ACE.OLEDB读取C#中的excel文件。我的Excel文件有以下表格,按以下顺序排列:

第1张,第1张(2),第2张

我的代码中的变量sheetName将"Sheet1(2)"作为第一个工作表,而不是"Sheet1"。

我的问题是它如何决定纸张的顺序?

string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
// Extra blank space cannot appear in Office 2007 and the last version. And we need to pay attention on semicolon.

using (OleDbConnection conn = new OleDbConnection(connstring))
{
    conn.Open();
    System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] {null, null, null, "Table"});
    string firstSheetName = sheetsName.Rows[0][2].ToString();
    string sql = string.Format("select * from [{0}]", firstSheetName);
    OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
    DataSet set = new DataSet();
    ada.Fill(set);

使用GetOleDbSchemaTable读取时Excel文件中的工作表顺序

根据这篇帖子,主持人说I am afraid that OLEDB does not preserve the sheet order as they were in Excel.

不过,在谷歌上搜索时,我发现了这个可能对你有所帮助的答案。