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

我使用如下的ACE.OLEDB来读取C#中的excel文件 . 我的Excel文件包含以下表格,顺序如下:

Sheet1,Sheet1(2),Sheet2

我的代码中的变量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);

回答(1)

2 years ago

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

虽然谷歌搜索,我发现this SO answer可能会帮助你 .