簡體   English   中英

如何使用C#在Excel范圍周圍添加邊框?

[英]How to add borders around Excel range using C#?

有人可以告訴我如何在另一種顏色的單元格外部添加邊框嗎? 理想情況下,我希望能夠使用一種方法來執行此操作,而我將不得不多次執行此操作。 搜索此內容后,我發現顯然可以執行此操作的兩個方法BorderAroundBorderAround2 我想我的第一個問題是這兩種方法有什么區別? 我嘗試使用其中的每一個,但只能識別BorderAround2嗎?

無論如何,“ BorderAround2”幾乎可以滿足我的要求。 我使用了以下代碼行,該代碼的確在范圍的外部放置了邊框,但它是黑色的,而不是紅色的:

ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexNone, Color.FromArgb(255, 0, 0), Type.Missing);

此方法的MSDN文檔指出:

您必須指定ColorIndex或Color,但不能同時指定。

我該怎么做呢? 如果將ColourIndex參數設置為Type.Missing或將其設置為null或完全錯過它,則會產生錯誤。 任何幫助,將不勝感激。

最后,我要指出的是,我在這里找到了一種解決方法,可以其中單獨設置各個邊的集合,但是正如我所說的,我希望使用一種方法來執行此操作,因為它必須重復多次。

要將邊界添加到Excel Range(單元格的范圍,通常可以由1 ..很多行和1 ..很多列組成)的一側或多側,但是對於這種特定情況,我們可能希望堅持使用一行和1..many列),您只需要做三件事:

0) Define the range
1) Get a reference to the Range's Borders array
2) Assign a border to one or more of the Border array's edges/sides (top, bottom, left, right)

首先,如下定義您要操作的范圍:

var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];

接下來,獲得對Range的Borders數組的引用,如下所示:

Borders border = rowToBottomBorderizeRange.Borders;

最后,將邊框分配給Border數組的一個或多個邊緣; 例如,如果要在底部添加邊框,如下所示:

border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

放在一起,代碼可能是:

var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Borders border = rowToBottomBorderizeRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

如果您在多個地方執行此操作,則可以使用該方法:

private void AddBottomBorder(int rowToBottomBorderize)
{
    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
    Borders border = rowToBottomBorderizeRange.Borders;
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
}

上面的示例僅顯示了底部邊框,但是您可以輕松添加頂部,左側或右側邊框線,將“ xlEdgeBottom”替換為“ xlEdgeTop”,“ xlEdgeRight”或“ xlEdgeLeft”

或者,您可以在以下范圍內添加邊框:

private void Add360Borders(int rowToBorderize)
{
    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBorderize, TOTALS_COL]];
    Borders border = rowToBorderizeRange.Borders;
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
}

注意:您將需要像這樣定義工作表:

private Worksheet _xlSheet;

...並在您的解決方案中引用Microsoft.Office.Interop.Excel程序集。

嘗試:

ws.get_Range("B2", "E3").Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
ws.get_Range("B2", "E3").Borders.Color = ColorTranslator.ToOle(Color.Red);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM