如何通过COM Interop在Excel中获取特定范围?
我有以下问题。 我必须通过COM互操作来读取一个excel文件。 我是使用COM interop编程的新手。
我用这个search一个特定的string:
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);
不,我想用foundRange作为起点来获得另一个范围。
像这样的东西
Excel.Range MyRange = this.sheet.Range[foundRange + 2 rows, + 1 column & lastRow];
我没有办法做到这一点。 有一个吗?
好吧,睡一觉后,我find了答案。
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];