jan*_*b04 0 c# gembox-spreadsheet
我有个问题。有没有办法可以使用 for 循环遍历电子表格中的所有列/行?现在我在我的代码中使用这样的 foreach 循环:(你可以忽略里面发生的事情)。
foreach (ExcelRow row in w1.Rows)
{
foreach (ExcelCell cell in row.AllocatedCells)
{
Console.Write("row: {0}", globalVar.iRowActual);
if (globalVar.iRowActual > 1)
{
cellValue = SafeCellValue(cell);
Console.WriteLine("value is: {0}", cellValue);
}
}
globalVar.iRowActual++;
}
Run Code Online (Sandbox Code Playgroud)
问题是我想将每个单元格的值分配给一个新变量并将其传递给另一个方法。我想为此使用for循环,我知道我可以使用CalculateMaxUsedColumns作为cols的限制,但是是否有这样的属性,我可以用于行?!
这就是我想做的:
int columnCount = ws.CalculateMaxUsedColumns();
int rowCount = ws.CalculateMaxUsedRows(); ------> PART I NEED HELP WITH
for(int i=0; i <columnCount; i++){
for(int j = 0; j<rowCount; j++){
.....
}
}
Run Code Online (Sandbox Code Playgroud)
任何形式的帮助将不胜感激。谢谢!!!
这是一种您可以在 GemBox.Spreadsheet 中使用循环遍历电子表格中所有列/行的方法for。通过ExcelWorksheet.GetUsedCellRange方法返回的CellRange。
ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet worksheet = workbook.Worksheets[0];
CellRange range = worksheet.GetUsedCellRange(true);
for (int r = range.FirstRowIndex; r <= range.LastRowIndex; r++)
{
for (int c = range.FirstColumnIndex; c <= range.LastColumnIndex; c++)
{
ExcelCell cell = range[r - range.FirstRowIndex, c - range.FirstColumnIndex];
string cellName = CellRange.RowColumnToPosition(r, c);
string cellRow = ExcelRowCollection.RowIndexToName(r);
string cellColumn = ExcelColumnCollection.ColumnIndexToName(c);
Console.WriteLine(string.Format("Cell name: {1}{0}Cell row: {2}{0}Cell column: {3}{0}Cell value: {4}{0}",
Environment.NewLine, cellName, cellRow, cellColumn, (cell.Value) ?? "Empty"));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
在较新的版本中,有一些额外的 API 可以简化这一点。例如,你现在可以使用foreach,并且仍然retreive行和列的索引与ExcelCell.Row.Index和ExcelCell.Column.Index您可以retreive名称,而不使用这些静态方法(不RowColumnToPosition,RowIndexToName和ColumnIndexToName)。
ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet worksheet = workbook.Worksheets[0];
foreach (ExcelRow row in worksheet.Rows)
{
foreach (ExcelCell cell in row.AllocatedCells)
{
Console.WriteLine($"Cell value: {cell.Value ?? "Empty"}");
Console.WriteLine($"Cell name: {cell.Name}");
Console.WriteLine($"Row index: {cell.Row.Index}");
Console.WriteLine($"Row name: {cell.Row.Name}");
Console.WriteLine($"Column index: {cell.Column.Index}");
Console.WriteLine($"Column name: {cell.Column.Name}");
Console.WriteLine();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,还有另外两种方法可以在for循环中迭代工作表单元格。
1)使用ExcelWorksheets.Rows.Count和ExcelWorksheets.CalculateMaxUsedColumns()来获取最后使用的行和列。
ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet worksheet = workbook.Worksheets[0];
int rowCount = worksheet.Rows.Count;
int columnCount = worksheet.CalculateMaxUsedColumns();
for (int r = 0; r < rowCount; r++)
{
for (int c = 0; c < columnCount; c++)
{
ExcelCell cell = worksheet.Cells[r, c];
Console.WriteLine($"Cell value: {cell.Value ?? "Empty"}");
Console.WriteLine($"Cell name: {cell.Name}");
Console.WriteLine($"Row name: {cell.Row.Name}");
Console.WriteLine($"Column name: {cell.Column.Name}");
Console.WriteLine();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有一个非统一电子表格,其中行具有不同的列数(例如,第一行有 10 个单元格,第二行有 100 个单元格等),那么您可以使用以下更改以避免迭代非-分配的单元格:
int rowCount = worksheet.Rows.Count;
for (int r = 0; r < rowCount; r++)
{
ExcelRow row = worksheet.Rows[r];
int columnCount = row.AllocatedCells.Count;
for (int c = 0; c < columnCount; c++)
{
ExcelCell cell = row.Cells[c];
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
2)使用CellRange.GetReadEnumerator方法,它只遍历范围内已分配的单元格。
ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet worksheet = workbook.Worksheets[0];
CellRangeEnumerator enumerator = worksheet.Cells.GetReadEnumerator();
while (enumerator.MoveNext())
{
ExcelCell cell = enumerator.Current;
Console.WriteLine($"Cell value: {cell.Value ?? "Empty"}");
Console.WriteLine($"Cell name: {cell.Name}");
Console.WriteLine($"Row name: {cell.Row.Name}");
Console.WriteLine($"Column name: {cell.Column.Name}");
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)