简体   繁体   English

如何遍历Aspose.Cells for .Net中命名范围内的所有单元格?

[英]How do iterate over all cells in a named range in Aspose.Cells for .Net?

I'm working with .Net Apose.Cells. 我正在使用.Net Apose.Cells。 I am trying to figure out to iterate over all the cells in a named range. 我试图找出迭代命名范围内的所有单元格。

I retrieve the named range from the workbook: 我从工作簿中检索命名范围:

Range range = AsposeWorkbook.Worksheets.GetRangeByName(rangeName);

I would like to set the value for each cell in the named range to zero (0). 我想将命名范围内每个单元格的值设置为零(0)。 How do I do this? 我该怎么做呢? What is the best method for iterating over cells in a named range? 迭代命名范围内的单元格的最佳方法是什么?

Thanks, JB 谢谢,JB

You can iterate the cells of your range using the Range.GetEnumerator() method. 您可以使用Range.GetEnumerator()方法迭代范围内的单元格。 Once you have access to your Cell object, then you can set its value to 0 using Cell.PutValue() method. 一旦可以访问Cell对象,则可以使用Cell.PutValue()方法将其值设置为0。 Please see the following sample code, its comments and example output 请参阅以下示例代码,其注释和示例输出

//Load your excel file
Workbook workbook = new Workbook("s1.xlsx");

//Access first worksheet
Worksheet ws = workbook.Worksheets[0];

//THis way you can create range on runtime
Range rn = ws.Cells.CreateRange("A1:D6");
rn.Name = "MyRange";

//Access your range as you have been doing before
Range range = workbook.Worksheets.GetRangeByName("MyRange");

//Iterate all the cells in your range, print their names and values
IEnumerator e = range.GetEnumerator();

while (e.MoveNext())
{
    Cell c = (Cell)e.Current;
    Console.WriteLine(c.Name + ": " + c.StringValue);

    //Set value to 0
    c.PutValue(0);
}

Example Console Output 控制台输出示例

A1: 27
B1: 92
C1: 58
D1: 58
A2: 75
B2: 21
C2: 61
D2: 27
A3: 55
B3: 49
C3: 73
D3: 92
A4: 6
B4: 7
C4: 2
D4: 62
A5: 18
B5: 75
C5: 76
D5: 32
A6: 32
B6: 63
C6: 18
D6: 34

Note: I am working as Developer Evangelist at Aspose 注意:我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM