Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Border in Excel left, right, bottom and top

Tags:

c#

excel

border

Firstly, I changed color borders of my sheet to white, because I want to have a white sheet.

Then I made some headers and want to make a border around it. The problem is that it made borders between the values in header, but top, down are not visible.

My code:

xlWorkSheet5.Columns.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); // Color Sheet5 to white, BusLoad
xlWorkSheet5.Columns.NumberFormat = "@";

Excel.Range rng = (Excel.Range)xlWorkSheet5.get_Range("A7","J7");
rng.RowHeight = 25.5;

rng.BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlHairline, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
rng.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
rng.Borders.Weight = 1d;

rng.Font.Bold = true;
rng.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray); 
like image 699
Le Viet Hung Avatar asked Dec 11 '12 09:12

Le Viet Hung


People also ask

How do I change the border style in Excel?

Select one or more cells that have a border that you want to change. Right-click over the cells you've chosen and select Format Cells and, in the popup window, click the Border tab. For a continuous line, choose one of the thicker styles from the Line box. In the Presets section, click your existing border type.

How do I restore the normal border in Excel?

Click on the View tab, then check the box for Gridlines in the Show group.


2 Answers

OK I found a solution to do it, here is my code:

xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = 1d;
xlWorkSheet5.Cells[7, 1].Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = 1d;
xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = 1d;
xlWorkSheet5.Cells[7,1].Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = 1d;
like image 161
Le Viet Hung Avatar answered Nov 03 '22 15:11

Le Viet Hung


If you want to use the Borders[index] property then use something along the lines of:

rng.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
rng.Borders[XlBordersIndex.xlEdgeLeft].ColorIndex = <COLOR THAT YOU WANT>

rng.Borders[XlBordersIndex.xlEdgeTop]...
rng.Borders[XlBordersIndex.xlEdgeBottom]...
rng.Borders[XlBordersIndex.xlEdgeRight]...
like image 30
K_B Avatar answered Nov 03 '22 17:11

K_B