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

时间:2014-10-01 15:02:19

标签: c# excel oledbconnection

我使用如下的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 个答案:

答案 0 :(得分:0)

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

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