如何通过COM Interop在Excel中获取特定范围?

mrt*_*181 7 c# excel range com-interop

我有以下问题.我必须通过COM interop读取excel文件.我是COM interop编程的新手.

我用这个搜索一个特定的字符串:

this.sheet = (Excel.Worksheet)this.excelApp.Workbook.Sheets.Item[this.sheetname];
            this.sheet.Activate();
            Excel.Range firstRow = this.sheet.Range["A1", "XFD1"];
            Excel.Range foundRange = firstRow.Find(
                this.StringISearch,
                Type.Missing,
                Type.Missing,
                Excel.XlLookAt.xlWhole,
                Excel.XlSearchOrder.xlByColumns,
                Excel.XlSearchDirection.xlNext,
                false,
                false,
                Type.Missing);
Run Code Online (Sandbox Code Playgroud)

不,我想使用foundRange作为获得另一个范围的起点.

像这样的东西

Excel.Range MyRange = this.sheet.Range[foundRange + 2 rows, + 1 column & lastRow];
Run Code Online (Sandbox Code Playgroud)

我没有办法做到这一点.有吗?

mrt*_*181 28

好吧,经过一段时间的睡眠我找到了答案.

 int startColumn = Header.Cells.Column;
 int startRow = header.Cells.Row + 1;
 Excel.Range startCell = this.sheet.Cells[startRow, startColumn];
 int endColumn = startColumn + 1;
 int endRow = 65536;
 Excel.Range endCell = this.sheet.Cells[endRow, endColumn];
 Excel.Range myRange = this.sheet.Range[startCell, endCell];
Run Code Online (Sandbox Code Playgroud)