1 回答
TA贡献1804条经验 获得超8个赞
在Excel中,您可以拥有像这样的复杂范围字符串:“D6:F11,I6:I9,J14,N10,P5:Q9”,因此首先您需要用“,”分割,然后对于每个包含“:”的子字符串,您需要获取单元格的子范围。
Hear 是一种获取范围字符串、解析它并返回单元格字符串列表的方法:
private static List<string> GetCellStrings(string cellRange)
{
// Cell string to return from this method.
List<string> cells = new List<string>();
// Remove whitespace.
cellRange = cellRange.Replace(" ", "").Trim();
// First split by ',' to get subranges
string[] subranges = cellRange.Split(',');
// Iterate over subranges
for (int i = 0; i < subranges.Length; i++)
{
string range = subranges[i];
// If the subrange contains a ':', calculate all range cells
if (range.Contains(':'))
{
string[] rangeBounds = range.Split(':');
char lowerBoundLetter = rangeBounds[0][0];
char upperBoundLetter = rangeBounds[1][0];
int lowerBoundNumber = int.Parse(rangeBounds[0].Substring(1));
int upperBoundNumber = int.Parse(rangeBounds[1].Substring(1));
for (char columnLetter = lowerBoundLetter; columnLetter <= upperBoundLetter; columnLetter++)
{
for (int rowNumber = lowerBoundNumber; rowNumber <= upperBoundNumber; rowNumber++)
{
string cell = columnLetter.ToString() + rowNumber.ToString();
cells.Add(cell);
}
}
}
// If the subrange does not contain a ':', it's a single cell, add it to the list of cells
else
{
cells.Add(range);
}
}
return cells;
}
注意:此代码仅适用于 26 列 - A 到 Z。
例子:
string cellRange = "D6:F11,I6:I9,J14,N10,P5:Q9";
List<string> cells = GetCellStrings(cellRange);
for (int i = 0; i < cells.Count; i++)
{
Console.WriteLine(cells[i]);
}
输出:
D6
D7
D8
D9
D10
D11
E6
E7
E8
E9
E10
E11
F6
F7
F8
F9
F10
F11
I6
I7
I8
I9
J14
N10
P5
P6
P7
P8
P9
Q5
Q6
Q7
Q8
Q9
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报