NPOI读取excel sheet名称列表
时间: 2024-09-09 18:01:01 浏览: 96
使用NPOI读取Excel文件时,你可以通过HSSFWorkbook(含工作簿)类来访问Excel的工作表。以下是如何获取Sheet名称列表的示例:
```csharp
using NPOI.HSSF.UserModel;
using NPOI.OpenXml4Net.OPC;
// 引入所需的NPOI库
var package = new Package(new FileInfo("your_file.xlsx")); // 替换为你的Excel文件路径
var workbook = package.GetParts().Where(part => part.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").SelectMany(p => p.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook")).Single().TargetUri.Value;
var excelWorkbook = HSSFWorkbook.LoadFromStream(package.GetPart(workbook).InputStream);
// 获取Sheet名称列表
var sheetNames = excelWorkbook.SheetNames;
foreach (string sheetName in sheetNames)
{
Console.WriteLine(sheetName); // 打印每个Sheet的名称
}
package.Close(); // 关闭包装
```
阅读全文
相关推荐



















