如何使用 C# 有效地将右边框添加到 Excel 范围?

How can I efficiently add right borders to an Excel range with C#?

是否有一种简洁的(单行)方法可以使用 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;
        }
    }
}

...(外循环覆盖行范围,内循环覆盖列范围),但看起来有点矫枉过正,而且效率可能很低。

这样效率更高,交叉眼更少:

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