如何获取 excel 中所有工作表的名称
How to get names of all sheets in excel
我想创建一个方法来获取工作簿中所有工作表的名称。我的工作簿有 7 张。如果我想读取工作表名称并将其保存到变量 excelSheets,我会收到 9 个名称,其中两个名称响应不存在的工作表("lists$" 和 "TYPAB")。
我不明白问题在哪里?如何仅获取现有工作表的名称?
public List<string> NamesOfSheets(string filename)
{
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
List<string> excelSheets;
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}
为什么不为此使用 Office Interop?
foreach (Excel.Worksheet displayWorksheet in Globals.ThisWorkbook.Worksheets)
{
Debug.WriteLine(displayWorksheet.Name);
}
Oscar,谢谢你的帮助,但是office interlop并没有解决我的问题。
我发现 "lists$" 是隐藏的 sheet,所以只有名称 TYPAB 不响应任何现有的 sheet。
所以我添加了子句 where 和 problem is solved。 :)
public List<string> NamesOfSheets(string filename)
{
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
List<string> excelSheets;
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Where(i => i["TABLE_NAME"].ToString().EndsWith("$") || i["TABLE_NAME"].ToString().EndsWith("$'"))
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}
我想创建一个方法来获取工作簿中所有工作表的名称。我的工作簿有 7 张。如果我想读取工作表名称并将其保存到变量 excelSheets,我会收到 9 个名称,其中两个名称响应不存在的工作表("lists$" 和 "TYPAB")。
我不明白问题在哪里?如何仅获取现有工作表的名称?
public List<string> NamesOfSheets(string filename)
{
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
List<string> excelSheets;
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}
为什么不为此使用 Office Interop?
foreach (Excel.Worksheet displayWorksheet in Globals.ThisWorkbook.Worksheets)
{
Debug.WriteLine(displayWorksheet.Name);
}
Oscar,谢谢你的帮助,但是office interlop并没有解决我的问题。 我发现 "lists$" 是隐藏的 sheet,所以只有名称 TYPAB 不响应任何现有的 sheet。
所以我添加了子句 where 和 problem is solved。 :)
public List<string> NamesOfSheets(string filename)
{
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
List<string> excelSheets;
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Where(i => i["TABLE_NAME"].ToString().EndsWith("$") || i["TABLE_NAME"].ToString().EndsWith("$'"))
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}