如何在.xls文件中捕获WorkBook名称?

时间:2012-02-02 02:16:04

标签: c# asp.net xls

以下文件示例:

enter image description here

我想将“客户”(突出显示)捕获为string

如何在ASP.NET / C中实现这一目标?

谢谢。

2 个答案:

答案 0 :(得分:1)

    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;
    }

答案 1 :(得分:1)

您可以使用OleDbConnection.GetOleDbSchemaTable方法。

 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"]);
   }