如何以及在何处使用枚举器
Contents
[
Hide
]
枚举数是一个对象,它提供了遍历容器或集合的能力。枚举器可用于读取集合中的数据,但不能用于修改底层集合,而 IEnumerable 是一个接口,它定义了一个方法 GetEnumerator,该方法返回一个 IEnumerator 接口,这反过来又允许只读访问一个集合。
Aspose.Cells API 提供了一堆枚举器,但是本文主要讨论下面列出的三种类型。
- Cells 枚举器
- 行枚举器
- 列枚举器
如何使用枚举器
Cells 枚举器
访问 Cells 枚举器的方法有很多种,可以根据应用程序的要求使用其中的任何一种方法。以下是返回单元格枚举器的方法。
所有上述方法都返回允许遍历已初始化单元格集合的枚举器。
在遍历单元格时,不应修改集合(将导致实例化新的 Cell 或删除现有的 Cell 的操作)。否则,枚举器可能无法正确遍历所有单元格(可能会重复遍历或跳过某些元素)。
下面的代码示例演示了 Cells 集合的 IEnumerator 接口的实现。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a file in an instance of Workbook | |
var book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the enumerator from Cells collection | |
IEnumerator cellEnumerator = book.Worksheets[0].Cells.GetEnumerator(); | |
// Traverse cells in the collection | |
while (cellEnumerator.MoveNext()) | |
{ | |
var cell = cellEnumerator.Current as Aspose.Cells.Cell; | |
Console.WriteLine(cell.Name + " " + cell.Value); | |
} | |
// Get enumerator from an object of Row | |
IEnumerator rowEnumerator = book.Worksheets[0].Cells.Rows[0].GetEnumerator(); | |
// Traverse cells in the given row | |
while (rowEnumerator.MoveNext()) | |
{ | |
var cell = rowEnumerator.Current as Aspose.Cells.Cell; | |
Console.WriteLine(cell.Name + " " + cell.Value); | |
} | |
// Get enumerator from an object of Range | |
IEnumerator rangeEnumerator = book.Worksheets[0].Cells.CreateRange("A1:B10").GetEnumerator(); | |
// Traverse cells in the range | |
while (rangeEnumerator.MoveNext()) | |
{ | |
var cell = rangeEnumerator.Current as Aspose.Cells.Cell; | |
Console.WriteLine(cell.Name + " " + cell.Value); | |
} |
行枚举器
可以在使用时访问行枚举器行集合.GetEnumerator方法。下面的代码示例演示了 IEnumerator 接口的实现行集合.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Load a file in an instance of Workbook | |
var book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the enumerator for RowCollection | |
IEnumerator rowsEnumerator = book.Worksheets[0].Cells.Rows.GetEnumerator(); | |
// Traverse rows in the collection | |
while (rowsEnumerator.MoveNext()) | |
{ | |
var row = rowsEnumerator.Current as Aspose.Cells.Row; | |
Console.WriteLine(row.Index); | |
} |
列枚举器
可以在使用时访问列枚举器ColumnCollection.GetEnumerator方法。下面的代码示例演示了 IEnumerator 接口的实现列集合.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Load a file in an instance of Workbook | |
var book = new Workbook(dataDir + "sample.xlsx"); | |
// Get the enumerator for ColumnsCollection | |
IEnumerator colsEnumerator = book.Worksheets[0].Cells.Columns.GetEnumerator(); | |
// Traverse columns in the collection | |
while (colsEnumerator.MoveNext()) | |
{ | |
var col = colsEnumerator.Current as Aspose.Cells.Column; | |
Console.WriteLine(col.Index); | |
} |
在哪里使用枚举器
为了讨论使用枚举器的优势,让我们举一个实时示例。
设想
一个应用需求是遍历给定的所有单元格工作表读取它们的值。可以有几种方法来实现这个目标。下面展示了一些。
使用显示范围
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Load a file in an instance of Workbook | |
var book = new Workbook(dataDir + "sample.xlsx"); | |
// Get Cells collection of first worksheet | |
var cells = book.Worksheets[0].Cells; | |
// Get the MaxDisplayRange | |
var displayRange = cells.MaxDisplayRange; | |
// Loop over all cells in the MaxDisplayRange | |
for (int row = displayRange.FirstRow; row < displayRange.RowCount; row++) | |
{ | |
for (int col = displayRange.FirstColumn; col < displayRange.ColumnCount; col++) | |
{ | |
// Read the Cell value | |
Console.WriteLine(displayRange[row, col].StringValue); | |
} | |
} |
使用 MaxDataRow 和 MaxDataColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Load a file in an instance of Workbook | |
var book = new Workbook(dataDir + "sample.xlsx"); | |
// Get Cells collection of first worksheet | |
var cells2 = book.Worksheets[0].Cells; | |
int maxDataRow = cells2.MaxDataRow; | |
int maxDataColumn = cells2.MaxDataColumn; | |
// Loop over all cells | |
for (int row = 0; row <= maxDataRow; row++) | |
{ | |
for (int col = 0; col <= maxDataColumn; col++) | |
{ | |
// Read the Cell value | |
var currentCell = cells2.CheckCell(row, col); | |
if (currentCell != null) | |
{ | |
Console.WriteLine(currentCell.StringValue); | |
} | |
} | |
} |
正如您所观察到的,上述两种方法都或多或少地使用了相似的逻辑,即;遍历集合中的所有单元格以读取单元格值。由于如下所述的多种原因,这可能会产生问题。
- API,例如最大行数, 最大数据行, 最大列, 最大数据列 & 最大显示范围需要额外的时间来收集相应的统计数据。如果数据矩阵(行 x 列)很大,使用这些 API 可能会造成性能损失。
- 在大多数情况下,并非给定范围内的所有单元格都被实例化。在这种情况下,与仅检查初始化单元格相比,检查矩阵中的每个单元格效率不高。
- 以Cells row,column的形式循环访问一个cell会导致一个范围内的所有cell对象都被实例化,最终可能导致OutOfMemoryException。
结论
基于上述事实,以下是应该使用枚举器的可能场景。
- 需要对单元格集合进行只读访问,即;要求是只检查细胞。
- 要遍历大量的单元格。
- 只遍历初始化的单元格/行/列。