我们可以在OpenXML中使用索引和.Count吗? [英] Can we use indexing and .Count with OpenXML?
问题描述
我被告知 OpenXML 非常快,我正在将其中一个应用移植到它。其中一个操作很常见:迭代工作表名称。我习惯用for循环做这种事情。
I am told that OpenXML is very fast and am porting one of my apps to it. One of the operations is rather common: to iterate through the worksheet names. I am used to doing this sort of thing with a for loop.
但是,我在这里找到的例子:
However, the example that I found here:
https:// docs .microsoft.com / zh-CN / office / open-xml / how-to-retrieve-a-the-the-worksheets-in-a-spreadsheet
https://docs.microsoft.com/en-us/office/open-xml/how-to-retrieve-a-list-of-the-worksheets-in-a-spreadsheet
执行迭代使用foreach()语句。
performs the iteration using a foreach() statement.
static void Main(string[] args)
{
var results = GetAllWorksheets(DEMOFILE);
foreach (Sheet item in results)
{
Console.WriteLine(item.Name);
}
}
我绝对偏爱for循环,并能够.Count项目。这些基本功能是否缺少
OpenXML ?
I am definitely partial to for loops, and being able to .Count items. Are those essential features missing from
OpenXML?
TIA
推荐答案
似乎与索引相关的任何内容都不可用?此代码不起作用:
It seems that anything related to indexing is not available? This code does not work:
int sheetIndex = 0;
foreach (WorksheetPart worksheetpart in workbook.WorkbookPart.WorksheetParts)
{
Worksheet worksheet = worksheetpart.Worksheet;
// Grab the sheet name each time through your loop
string sheetName = workbookPart.Workbook.Descendants<Sheet>().ElementAt(sheetIndex).Name;
foreach (SheetData sheetData in worksheet.Elements<SheetData>())
{
...
}
sheetIndex++;
}
发布在这里:
https:/ /stackoverflow.com/questions/7504285/how-to-retrieve-tab-names-from-excel-sheet-using-openxml
https://stackoverflow.com/questions/7504285/how-to-retrieve-tab-names-from-excel-sheet-using-openxml
更具体地说,.ElementAt(索引)不是到达。也许我需要演员?
More specifically, the .ElementAt(index) is not reachable. Perhaps I need a cast?
TIA
这篇关于我们可以在OpenXML中使用索引和.Count吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!