Ranges and borders

Hi again,

I have a doubt regarding ranges and some borders… I was trying to apply some styles to a sheet and came across this issue: how do you set the inside borders of a range of cells?

Regards,
Cruz

Hi Cruz,
Thanks for considering Aspose.
May the following sample code help you for your need, kindly consult it:
Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange("B6", "P216");
Style stl;
stl= workbook.Styles[workbook.Styles.Add()];
stl.Font.Name = "Arial";
stl.Font.IsBold = true;
stl.Font.Color = Color.Blue;
stl.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.TopBorder].Color = Color.Blue;
stl.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.LeftBorder].Color = Color.Blue;
stl.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.BottomBorder].Color = Color.Blue;
stl.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.RightBorder].Color = Color.Blue;
range.Style = stl;
workbook.Save(@"d:\test\in_rangeborders.xls");
Thank you.

Thanks for the reply, Amjad.

Yes, that does format all borders in all cells in range but it does not preserve the original style of the cells, which I would like to. The thing is I have this predefined table with lots of different styles throughout the sheet, and I have to apply the borders after I build the table because it has a dynamic size and the only way I have to know it’s size is after it is built.

So this method doesn’t work for me because it overwrites the previous style of cells in the range. That’s why I was using the SetOutlineBorder method, but this method doesn’t apply the border to the “betweens” of the cells.

Is there another way to do this?

Thanks,
Cruz

Hi Cruz,

Well if you want to preserve the existing formattings in the template file, you may use StyleFlag struct. You will define a style object and set the borders, define style flag object and specify only the border to be implemented, finaly you may use Range.ApplyStyle(style, flag) method to only apply the formatting confirmed with flag.

May the following code help you for your requirement.

Workbook workbook = new Workbook();
workbook.Open("d:\\test\\input_book.xls");
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange("B6", "P216");
Style stl;
stl= workbook.Styles[workbook.Styles.Add()];
stl.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.TopBorder].Color = Color.Blue;
stl.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.LeftBorder].Color = Color.Blue;
stl.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.BottomBorder].Color = Color.Blue;
stl.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.RightBorder].Color = Color.Blue;
StyleFlag flag = new StyleFlag();
//Apply only borders(top, left, bottom, right) to the existing formattings
flag.Borders = true;
range.ApplyStyle(stl,flag);
Attached is a zip file containing input and output excel files, Please check it out.
Thank you.
workbook.Save(@"d:\test\ot_book.xls");

Thanks Amjad, that’s exactly it!

See you soon :wink:
Cruz