获取Excel范围对象的最后一个单元格(列,行) [英] Get the last cell (column, row) of a Excel range object

查看:347
本文介绍了获取Excel范围对象的最后一个单元格(列,行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的 Microsoft.Office.Interop.Excel.Range 对象,并希望估计范围,即(最后一栏的最后坐标,最后一行)。

I have a Microsoft.Office.Interop.Excel.Range object and want to estimate the end-coordinates of that range ie (last column, last row).

有像 LastCol LASTROW 没有属性。唯一的属性是,其中指定的第一个的细胞。 ?但是,我怎么能得到的范围内的最后一个单元格

There are no properties like LastCol, LastRow. The only properties are Column and Row, which specify the first cell. But how can I get the last cell of the range?

推荐答案

这应该得到的范围内的第一个第一和最后一个单元格:

This should get the first first and last cell of the range:


  1. 启动Excel中。

  1. Initiate Excel.

打开工作簿。

选择您的范围,在这种情况下,我得到了活动工作表的使用范围。

Select your range, in this case I got the used range of the active sheet.

通话范围的get_address方法。

Call the get_address method of the range.

拆分冒号get_address的结果。

Split the result of get_address on the colon.

从拆分结果将有开始单元数组的第一个值。

The first value of the array resulting from the split will have the beginning cell.

从拆分导致数组的第二个值会有结束的细胞。

The second value of the array resulting from the split will have the ending cell.

替换没事的美元符号,你将有范围的开始和结束的细胞。

Replace the dollar signs with nothing and you will have the beginning and ending cells for the range.

Microsoft.Office.Interop.Excel.Application excel = new Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
    excel.Workbooks.Open(fileLocation);
Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
string address = range.get_Address();
string[] cells = address.Split(new char[] {':'});
string beginCell = cells[0].Replace("$", "");
string endCell = cells[1].Replace("$", "");
workBook.Close(true);
excel.Quit();


如果你想最后一列和最后一行相对于你可以做以下的范围的开头:

If you want the last column and last row relative to the beginning of the range you can do the following:

    int lastColumn = range.Columns.Count;
    int lastRow = range.Rows.Count;

这篇关于获取Excel范围对象的最后一个单元格(列,行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆