[英]What is the easiest way to count .xlsx workbook sheets using c#, NPOI and an XSSF workbook?
I am trying to count the number of sheets in a workbook. 我正在尝试计算工作簿中的工作表数量。 The workbook is created using NPOI and there doesn't seem to be a way to count the amount of sheets using the C# version of NPOI? 使用NPOI创建工作簿,似乎没有办法使用NPOI的C#版本计算工作表数量?
This is a really tricky thing to both explain and show... But I will give it a try. 这对于解释和展示都是一件非常棘手的事情......但我会试一试。
What I am trying to do is having an existing excel-file as a template for statistics. 我想要做的是将现有的excel文件作为统计模板。 This existing excel-file can have different amounts of templates and I need to be able to count these templates to know where to place my new sheets and edit their names. 这个现有的excel文件可以有不同数量的模板,我需要能够计算这些模板,以了解放置新工作表的位置并编辑其名称。
The sender of the data only has to chose which template-sheet should be filled with which data, and I will then remove the template-sheets from the workbook after all data has been inserted. 数据的发送者只需选择哪个模板表应填充哪些数据,然后在插入所有数据后从工作簿中删除模板表。
What I have tried: 我尝试过的:
I have read the documentation and searched for information and have tried the following approaches: 我已阅读文档并搜索了相关信息,并尝试了以下方法:
Problem with this approach: The C# version of NPOI doesn't seem to have getNumberOfSheets. 这种方法的问题:NPOI的C#版本似乎没有getNumberOfSheets。
Can't really recreate the code to work for sheets as the functionality for sheets and rows are too different. 无法真正重新创建用于工作表的代码,因为工作表和行的功能太不同了。
var sheetIndex = 0;
foreach (var sheet in requestBody.Sheets)
{
if (sheet.TemplateNumber == "")
{
sheetTemplate = templateWorkbook.CreateSheet(sheet.Name);
}
else
{
sheetTemplate = templateWorkbook.CloneSheet(Convert.ToInt32(sheet.SheetTemplate));
if (!templates.Contains(Convert.ToInt32(sheet.SheetTemplate)))
{
templates.Add(Convert.ToInt32(sheet.SheetTemplate));
}
// Do math's to make sure we add the name to the newly created sheet further down the code (I need to actual index here)
}
// Insert statistics
//After inserting statistics:
workingCopy.SetSheetName(sheetIndex, sheet.Name);
foreach (var template in templates)
{
workingCopy.RemoveSheetAt(template);
}
}
您可以从XSSFWorkbook
类的NumberOfSheets
属性中XSSFWorkbook
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.