2010-11-30 50 views
7

我有以下问题。我必须通过COM互操作来读取一个excel文件。我是使用COM interop编程的新手。如何通过COM Interop在Excel中获取特定范围?

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

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

我不明白的方式来做到这一点。有一个吗?

回答

25

好的,经过一些睡眠后我找到了答案。

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]; 
相关问题