Microsoft.Office.Interop.Excel: How to Apply a bor

2019-04-26 21:42发布

I am looking to apply a border to one cell using the Microsoft.Office.Interop.Excel library.

I am running a while-loop that is search for a empty cell within a certain column, once the cell is found I want to apply a border to it.

I know there many forums on this using Ranges, but I can't use the range functionality since I do not know what cell it is being applied to exactly.

My idea was:

(Excel.Range)xlWS.Cells[row,1].Borders.LineStyle = (Whatever Value);

Any advice? (besides links to other forums, I already looked through tons of forms)?

4条回答
等我变得足够好
2楼-- · 2019-04-26 22:02
            Excel.Range range = xlWS.UsedRange;
            Excel.Range cell = range.Cells[row,column];
            Excel.Borders border = cell.Borders;

            border.LineStyle = Excel.XlLineStyle.xlContinuous;
            border.Weight = 2d;

Hope this helps someone! Puts a thin line border around cell[row,column]!

查看更多
\"骚年 ilove
3楼-- · 2019-04-26 22:03

This is based on jiverson's answer, but is much more concise for basic needs; simply create a range, get its Borders, and add a border to the bottom of the range:

var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]];
. . .
Borders border = platypusRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

Of course, you could add a border to the top and sides, too, using xlEdgeTop, xlEdgeLeft, and xlEdgeRight.

查看更多
劳资没心,怎么记你
4楼-- · 2019-04-26 22:10

To each cell in range

Microsoft.Office.Interop.Excel.Range chartRange;
chartRange = workSheet.get_Range("a1", "g7");
foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells)
{
    cell.BorderAround2();
}

To whole range

chartRange.BorderAround2();
查看更多
够拽才男人
5楼-- · 2019-04-26 22:13
    Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
    Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1];
    Microsoft.Office.Interop.Excel.Borders border = cell.Borders;
    border[XlBordersIndex.xlEdgeLeft].LineStyle =
        Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeTop].LineStyle =
        Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeBottom].LineStyle =
        Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeRight].LineStyle =
        Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

See this answer for more details.

查看更多
登录 后发表回答