2010-11-30 61 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]; 
相關問題