2011-09-14 45 views
1

如果您有一個單元格的引用,是否可以在Excel工作表中四處走動? 這樣的走動的Excel

Microsoft.Office.Interop.Excel.Range cellWalker = mfe.GetMyCell(Mysheet); 

cellWalker = cellWalker.GoUpOneRowButKeepColumn(); 
cellWalker = cellWalker.GoDownOneRowButKeepColumn(); 
cellWalker = cellWalker.GoLeftOneColumnButKeepRow(); 
cellWalker = cellWalker.GoRightOneColumnButKeepRow(); 

Regards Stefan

回答

3

Range.Offset屬性會爲你做到這一點。例如。

Microsoft.Office.Interop.Excel.Range cellWalker = mfe.GetMyCell(Mysheet); 

cellWalker = cellWalker.Offset[-1, 0]; // GoUpOneRowButKeepColumn 
cellWalker = cellWalker.Offset[1, 0]; // GoDownOneRowButKeepColumn 
cellWalker = cellWalker.Offset[0, -1]; // GoLeftOneColumnButKeepRow 
cellWalker = cellWalker.Offset[0, 1]; // GoRightOneColumnButKeepRow 
+0

這工作如想,thx –

0

該文檔說no。您可以通過將接口包裝到您自己的對象中來自己實現這些功能。您可以將這些方法寫入對象,然後使用「範圍」和「單元格」屬性來導航工作表。

概念(未經測試,但 「一般要點」 的例子)

public class ExcelWalker { 
    Microsoft.Office.Interop.Excel.Range navigator 
    Microsoft.Office.Interop.Excel.Range currentCells 
    Integer currentRow,currentColumn 

    public void GoUpOneRowButKeepColumn(){ 
     currentCells = navigator.Cells(currentRow++,currentColumn); 
    } 
}