【问题标题】:How can I efficiently add right borders to an Excel range with C#?如何使用 C# 有效地将右边框添加到 Excel 范围?
【发布时间】:2015-11-13 19:16:48
【问题描述】:

是否有一种简洁的(单行)方法可以使用 C# 以编程方式将右边框添加到 Excel 电子表格中的一系列单元格?

此代码有效:

private void AddRightBorderToMainRange()
{
    for (int i = COLUMN_HEADING_ROW; i < _lastRowAdded; i++)
    {
        for (int j = ITEMDESC_COL; j < TOTALS_COL; j++)
        {
            var rightBorderizeRange = _xlSheet.Range[_xlSheet.Cells[i, j], _xlSheet.Cells[i, j]];
            Borders border = rightBorderizeRange.Borders;
            border[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
        }
    }
}

...(覆盖行范围的外部 for 循环,覆盖列范围的内部循环),但似乎有点矫枉过正,可能效率低下。

【问题讨论】:

    标签: c# excel-interop memory-efficient named-ranges


    【解决方案1】:

    这样效率更高,而且不太容易引起交叉眼:

    var rightBorderizeRange = 
        _xlSheet.Range[_xlSheet.Cells[COLUMN_HEADING_ROW, ITEMDESC_COL], 
        _xlSheet.Cells[_lastRowAdded, TOTALS_COL]];
    Borders border = rightBorderizeRange.Borders;
    border[XlBordersIndex.xlInsideVertical].LineStyle =
        XlLineStyle.xlContinuous;
    

    那么需要做的是:

    0) Define the range you want to operate on (add right borderlines to) - named *rightBorderizeRange* above
    1) Get the Borders member for that range - named *border* above
    2) Apply a border to the InsideVertical (vertical lines within that range) member of the border array
    

    【讨论】:

    • 你能详细说明你做了什么吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多