Getting the first sheet from an Excel document regardless of sheet name with OleDb

前端 未结 9 695
青春惊慌失措
青春惊慌失措 2020-12-29 02:23

I have users that name their sheets all sorts of crazy things, but I want to be able to get the first sheet of the Excel document regardless of what it is named.

I

9条回答
  •  没有蜡笔的小新
    2020-12-29 02:32

    OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="Path"; Extended Properties=Excel 12.0;Persist Security Info=False;");
    
    oconn.Open();
    myCommand.Connection = oconn;
    DataTable dbSchema = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,  null);
    if (dbSchema == null || dbSchema.Rows.Count < 1)
    {
        throw new Exception("Error: Could not determine the name of the first worksheet.");
    }
    string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
    

提交回复
热议问题