How can a border be set around multiple cells in excel using C#

Viewed 61592

I am working on a project that creates excel files.

I am having trouble placing a border on multiple cells to organize the excel file.

Let's say I want a border from cell B5 to B10. There shouldn't be borders between B5, B6, B7,...

Currently, I have this code:

workSheet_range = worksheet.get_Range("B5", "B10");
workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();

It makes the borders, however it places a border around every cell instead of one big border for all cells.

How can I accomplish this?

8 Answers

Here is my solution, use simply UsedRange() function

Excel.Range tRange = oSheet.UsedRange;
            tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
            tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;        
ws.UsedRange.BorderAround(
                        Xl.XlLineStyle.xlDash,
                        Xl.XlBorderWeight.xlThick,
                        Xl.XlColorIndex.xlColorIndexAutomatic,
                        ColorTranslator.ToOle(Color.Blue));
Related