开发者

How to get specific Range in Excel through COM Interop?

开发者 https://www.devze.com 2023-01-28 10:54 出处:网络
I have to read an excel file through a COM interop. I search for a specific string using this: this.sheet = (Excel.Worksheet)this.excelApp.Workbook.Sheets.Item[this.sheetname];

I have to read an excel file through a COM interop. I search for a specific string using this:

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,
        开发者_如何学JAVA        Type.Missing,
                Excel.XlLookAt.xlWhole,
                Excel.XlSearchOrder.xlByColumns,
                Excel.XlSearchDirection.xlNext,
                false,
                false,
                Type.Missing);

Now I want to use the foundRange as a starting point to get another range.

Something like this

Excel.Range MyRange = this.sheet.Range[foundRange + 2 rows, + 1 column & lastRow];

I don't see a way to do this. Is there one?


Okay, after some sleep i have found the answer.

 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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号