rofans91
rofans91

Reputation: 3010

How to capture WorkBook Name in an .xls file?

Example for file below:

enter image description here

I want to capture the "Customers" (highlighted) as a string.

How do I achieve this in ASP.NET/C#?

Thank you.

Upvotes: 0

Views: 109

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

You may use OleDbConnection.GetOleDbSchemaTable method.

 connection.Open();
        DataTable schemaTable = connection.GetOleDbSchemaTable(
            OleDbSchemaGuid.Tables,
            new object[] { null, null, null, "TABLE" });
 foreach (DataRow row in schemaTable.Rows )
  {
     Console.WriteLine(row["TABLE_NAME"]);
   }

Upvotes: 1

Malk
Malk

Reputation: 11983

    public static List<string> GetSheetNames(string PathToExcelFile)
    {
        List<string> SheetNameList = new List<string>();

        System.Data.DataTable SchemaTable;

        string OleConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + PathToExcelFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";

        using (System.Data.OleDb.OleDbConnection OleConnection = new System.Data.OleDb.OleDbConnection(OleConnectionString))
        {
            OleConnection.Open();
            SchemaTable = OleConnection.GetSchema("Tables");
            OleConnection.Close();
        }
        foreach (System.Data.DataRow SchemaRow in SchemaTable.Rows)
        {
            SheetNameList.Add(SchemaRow["TABLE_NAME"].ToString().TrimEnd('$'));
        }

        return SheetNameList;
    }

Upvotes: 1

Related Questions