获取给定数据范围的以前的单元格

我尝试获取给定范围的前一个单元格。 到目前为止,我的代码如下所示:

获取选定的范围并将其传递给另一个方法

Microsoft.Office.Interop.Excel._Application app = this.ExcelAppObj as Microsoft.Office.Interop.Excel._Application; Microsoft.Office.Interop.Excel.Range range = null; range = app.get_Range(this.DataRangeTextBox.Text); var caption = ExcelHelper.GetRangeHeaderCaption(range); 

以下方法被执行

  /// <summary> /// Gets the range header caption of a given range. /// The methode goes through every previous cell of the given range to determine the caption. /// If the caption can not be determined the method returns an random string. /// </summary> /// <param name="selectedRange">The selected range.</param> /// <returns></returns> public static string GetRangeHeaderCaption(Range selectedRange, Microsoft.Office.Interop.Excel._Application excelApp) { // The caption of the range. The default value is a random string var rangeCaption = ExcelHelper.getRandomString(5); // Check if the provided range is valid if (selectedRange != null && excelApp.WorksheetFunction.CountA(selectedRange) > 0) { var captionNotFound = true; Range rangeToCheck = selectedRange.Previous; // Go to each previous cell of the provided range // to determine the caption of the range do { // No further previous cells found // We can stop further processing if (rangeToCheck.Cells.Count == 0) { break; } //System.Array myvalues = (System.Array)rangeToCheck.Cells.Value; System.Array myvalues = (System.Array)rangeToCheck.Cells.Value; rangeToCheck = rangeToCheck.Previous; } while (captionNotFound); } return rangeCaption; } 

在这一点上

 var rangeToCheck = selectedRange.Previous; 

属性访问会引发以下exception:

 A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Die Previous-Eigenschaft des Range-Objektes kann nicht zugeordnet werden. 

我想实现的是:遍历给定数据范围中包含double或int值的所有先前的单元格,并通过检查前一个单元格是数字值还是string来获取标题标题。 如果单元格包含一个string,则将该string返回给调用者。

编辑#1也许这是一个重要的信息。 GetRangeHeaderCaption方法在另一个类中实现。 它不包括在我通过使用Excel Interop获取范围的类中。

编辑#2发现问题。 属性Previous返回给定范围的前一个LEFT单元格。 例如,如果我的范围有地址B2:B16,则前一个属性返回地址A2。 所以,如果我尝试访问A2:A16的前一个属性,我得到的exception,因为在列A前没有列。

但我需要的是,如果我有范围B2:B16我需要得到B1的内容。 你可以跟我到目前为止?

那么,我用我的大脑为这个问题找出了一个解决scheme。 它的解决方法,我不知道是否有另一种方式或更好的方式来解决这个问题 – 看我的代码在这里:

  using XLS = Microsoft.Office.Interop.Excel; /// <summary> /// Gets the range header caption of a given range. /// The methode goes through every previous cell of the given range to determine the caption. /// If the caption can not be determined the method returns an random string. /// </summary> /// <param name="selectedRange">The selected range.</param> /// <returns></returns> public static string GetRangeHeaderCaption(Range selectedRange, XLS._Application excelApp) { // The caption of the range. The default value is a random string var rangeCaption = ExcelHelper.getRandomString(5); // Check if the provided range is valid if (selectedRange != null && excelApp.WorksheetFunction.CountA(selectedRange) > 0) { // Get the included cells of the range var rangeCells = selectedRange.Address.Split(new char[] { ':' }); // Get the beginning cell of the range var beginCell = rangeCells[0].Trim(); // Get the column and row data of the cell var cellColumnRow = beginCell.Split(new char[] { '$' }); // Split the beginning cell into the column and the row var cellColumn = cellColumnRow[1]; var cellRow = Convert.ToInt32(cellColumnRow[2]); var captionNotFound = true; int i = 0; // Go to each previous cell of the provided range // to determine the caption of the range do { // Check if the next cell would be invalid var nextCellRow = cellRow - i; if (nextCellRow == 0) break; // Create the cell coordinates to look at var cellToLook = string.Format("{0}{1}", cellColumn, nextCellRow); // Get the value out of the cell var cellRangeValue = ((XLS.Worksheet)((XLS.Workbook)excelApp.ActiveWorkbook).ActiveSheet).get_Range(cellToLook).Value; // ...just to be sure it does not crash if (cellRangeValue != null) { // Convert the determined value to an string var cellValue = cellRangeValue.ToString(); double value; // Check if the cell value is not a numeric value // Check if the cell value is not empty or null if (!double.TryParse(cellValue, out value) && !string.IsNullOrWhiteSpace(cellValue) && !string.IsNullOrEmpty(cellValue)) { // In this case we found the caption rangeCaption = cellValue; captionNotFound = false; } } i++; } while (captionNotFound); } return rangeCaption; }