/// <summary> /// 返回 范围内的 单元格集合,按照 列 优先 /// 该方法 不支持 合并单元格检测 /// </summary> /// <param name="ranges"></param> /// <returns></returns> public static IEnumerable <ExcelReference> Cells(this IEnumerable <ExcelReference> ranges) { ExcelReference[] references = ranges as ExcelReference[] ?? ranges.ToArray(); if (!references.Any()) { yield return(null); } if (references.Count() == 1) { ExcelReference range = references.FirstOrDefault(); int columns = range.Columns(); int rows = range.Rows(); for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { var row = range.RowFirst + r; var col = range.ColumnFirst + c; yield return(new ExcelReference(row, row, col, col, range.SheetId)); } } } else { int cellsCount = references.Sum(r => r.CellsCount()); int i = 0; int col = 0; while (i < cellsCount) { foreach (ExcelReference range in references) { int columns = range.Columns(); int rows = range.Rows(); if (col < columns) { for (int row = 0; row < rows; row++) { yield return (new ExcelReference(range.RowFirst + row, range.RowFirst + row, range.ColumnFirst + col, range.ColumnFirst + col, range.SheetId)); } } } col++; i++; } } }
public static int CellsCount(this ExcelReference range) { return(range.Rows() * range.Columns()); }