如何获取创建的对象“单元格”的范围?

我正在 Visual Studio 中设置一个 26x26 电子表格程序。我创建了一个cell继承 textbox 但还包含一个 property 的对象cellID。现在我创建了一个selectedRange文本框,允许用户输入将执行公式(例如:Sum)的范围(例如:“A1:A9”)。我想获取输入的范围并找到一种算术上使用它的方法。

我想也许我可以使用Split()带有 a':'','分隔符的方法,但我不知道如何构建它以便在我的代码中使用。

程序截图:

http://img1.mukewang.com/64bbac590001804b19191075.jpg

private void btn_sum_Click(object sender, EventArgs e)

{

    int rows = 0;

    int columns = 0; 


    string[] cells = txt_selectedRange.Text.Split(':',',');

}


森栏
浏览 65回答 1
1回答

胡说叔叔

在Excel中,您可以拥有像这样的复杂范围字符串:“D6:F11,I6:I9,J14,N10,P5:Q9”,因此首先您需要用“,”分割,然后对于每个包含“:”的子字符串,您需要获取单元格的子范围。Hear 是一种获取范围字符串、解析它并返回单元格字符串列表的方法:private static List<string> GetCellStrings(string cellRange){&nbsp; &nbsp; // Cell string to return from this method.&nbsp; &nbsp; List<string> cells = new List<string>();&nbsp; &nbsp; // Remove whitespace.&nbsp; &nbsp; cellRange = cellRange.Replace(" ", "").Trim();&nbsp; &nbsp; // First split by ',' to get subranges&nbsp; &nbsp; string[] subranges = cellRange.Split(',');&nbsp; &nbsp; // Iterate over subranges&nbsp; &nbsp; for (int i = 0; i < subranges.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string range = subranges[i];&nbsp; &nbsp; &nbsp; &nbsp; // If the subrange contains a ':', calculate all range cells&nbsp; &nbsp; &nbsp; &nbsp; if (range.Contains(':'))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] rangeBounds = range.Split(':');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char lowerBoundLetter = rangeBounds[0][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char upperBoundLetter = rangeBounds[1][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int lowerBoundNumber = int.Parse(rangeBounds[0].Substring(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int upperBoundNumber = int.Parse(rangeBounds[1].Substring(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (char columnLetter = lowerBoundLetter; columnLetter <= upperBoundLetter; columnLetter++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int rowNumber = lowerBoundNumber; rowNumber <= upperBoundNumber; rowNumber++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string cell = columnLetter.ToString() + rowNumber.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells.Add(cell);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // If the subrange does not contain a ':', it's a single cell, add it to the list of cells&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells.Add(range);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; 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++){&nbsp; &nbsp; Console.WriteLine(cells[i]);}输出:D6D7D8D9D10D11E6E7E8E9E10E11F6F7F8F9F10F11I6I7I8I9J14N10P5P6P7P8P9Q5Q6Q7Q8Q9
打开App,查看更多内容
随时随地看视频慕课网APP