Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Office.Interop.Excel: How to Apply a border to ONE CELL

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)?

like image 841
Van-Brad Avatar asked Jul 19 '13 16:07

Van-Brad


People also ask

Can you apply borders for a selected range of cells?

On a sheet, select the cell or range of cells where you want to add or change the borders. , and then click the cell border that you want to apply. Tip: To add or remove parts of a border, click More Borders at the bottom of the menu. Under Border, click the borders that you want to add or remove.


2 Answers

    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.

like image 102
jiverson Avatar answered Sep 21 '22 17:09

jiverson


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]!

like image 35
Van-Brad Avatar answered Sep 17 '22 17:09

Van-Brad