如何在Excel中的多行中获取值?

时间:2015-11-05 18:04:08

标签: c# excel office-interop excel-interop

我需要放置一个跨越四行的文本值。我认为/希望这会奏效:

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 个答案:

答案 0 :(得分: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;
}