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 b
// ** - You Should do it in all Cells
//BorderAround: Medium**
worksheet.get_Range("b5", "b5").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
worksheet.get_Range("b6", "b6").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
worksheet.get_Range("b10", "b10").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
Maybe this can help :
workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick);
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;
I did this without impacting the performance. I am taking a simple excel to format :
Before
I managed to store the range as A1:C4
in a variable dynamically in exRange and used the below code to give border
((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
After
ws.UsedRange.BorderAround(
Xl.XlLineStyle.xlDash,
Xl.XlBorderWeight.xlThick,
Xl.XlColorIndex.xlColorIndexAutomatic,
ColorTranslator.ToOle(Color.Blue));
You need to individually set these
.Borders[Excel.XlBordersIndex.xlEdgeBottom]
.Borders[Excel.XlBordersIndex.xlEdgeRight]
.Borders[Excel.XlBordersIndex.xlEdgeLeft]
.Borders[Excel.XlBordersIndex.xlEdgeTop]