2015-11-13 82 views
0

是否有一种简洁(单行)的方式,以编程方式将正确边框添加到使用C#的Excel电子表格上的一组单元格中?如何有效地向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循环覆盖范围的行,内环覆盖范围列),但似乎矫枉过正,可能效率低下。

回答

0

这一点更有效和更crosseye诱导:

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 
+0

你能请详细说明你做了什么?谢谢! –