locked
Excel Sheet RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below line . I want that it should read default Sheet1 

    OleDbCommand command = new OleDbCommand("select * from [Data$]", connection);

    Thanks

    Sunday, July 29, 2018 3:47 PM

Answers

  • User-1171043462 posted

    Reference: Read and Import Excel Sheet using ADO.Net and C#

    First Read the Schema of Excel file

    connExcel.Open();
    DataTable dtExcelSchema;
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    connExcel.Close();
    

    Then get the default Sheet name

    string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"];
    
    

    Then use in query

     string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
    OleDbCommand command = new OleDbCommand("select * from [" + SheetName  + "]", connection);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 29, 2018 3:52 PM