[英]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;
}
}
}
...(外部覆蓋行范圍的循環,內部覆蓋列范圍的循環),但看起來過於矯kill,可能效率不高。
這樣會更有效,也不會引起多視。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.