2015-11-05 27 views
1

我需要放置一個跨越四行的文本值。我想/希望這會工作:我如何獲得一個值來展開Excel中的多行?

private int curDescriptionTopRow = 8; 
. . . 
private void AddDescription(String desc) 
{ 
    int curDescriptionBottomRow = curDescriptionTopRow + 3; 

    _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].Font.Bold = true; 
    _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; 
    _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].Value2 = desc; 

    curDescriptionTopRow = curDescriptionTopRow + 4; 
} 

我需要的第一描述進行顯示,垂直居中,在細胞A8 - A11(A列,行8-11)。

上面的代碼在所有四行中添加了說明,而不是在四行中只有一次。

如何防止描述的三個冗餘外觀?

回答

1

定義,然後合併範圍的工作原理:

private int curDescriptionTopRow = 8; 
. . . 
private void AddDescription(String desc) 
{ 
    int curDescriptionBottomRow = curDescriptionTopRow + 3; 

    var range = 
     _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]]; 
    range.Merge(); 

    range.Font.Bold = true; 
    range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; 
    range.Value2 = desc; 
} 
相關問題