Le Viet Hung
Le Viet Hung

Reputation: 529

Change Border in Excel left, right, bottom and top

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

Upvotes: 8

Views: 36621

Answers (3)

user8434118
user8434118

Reputation: 11

Border.Weight property

XlBorderWeight can be one of these XlBorderWeight constants: xlHairline xlThin xlMedium xlThick.

Example for create continuous line on Cells from Bx to Mx, where x is number of line

using Excel = Microsoft.Office.Interop.Excel;

Excel.Range line = (Excel.Range)excelWorksheet.Rows[1]; 
line.Insert();

excelWorksheet.Range[$"B{1}:M{1}"].Cells.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
excelWorksheet.Range[$"B{1}:M{1}"].Cells.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;

Upvotes: 1

Le Viet Hung
Le Viet Hung

Reputation: 529

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;

Upvotes: 14

K_B
K_B

Reputation: 3678

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

Upvotes: 4

Related Questions