How can I set border around multiple cells in excel using c #

I am working on a project that creates excel files.

I find it difficult to place the border on multiple cells to organize the excel file.

Say I need a border from cell B5 to B10. There should be no boundaries between B5, B6, B7, ...

I currently have this code:

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

It creates borders, however it places a border around each cell, not one large border for all cells.

How can i do this?

+11
source share
8 answers

You need to individually set these

 .Borders[Excel.XlBordersIndex.xlEdgeBottom] .Borders[Excel.XlBordersIndex.xlEdgeRight] .Borders[Excel.XlBordersIndex.xlEdgeLeft] .Borders[Excel.XlBordersIndex.xlEdgeTop] 
+11
source

Perhaps this may help:

 workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick); 
+8
source

This is the code that sets the border around each cell:

 xlWS.get_Range("C9", "N9").Cells.Borders.Weight = XL.XlBorderWeight.xlMedium; 
+3
source

I did this without affecting performance. I take a simple form for formatting:

Before

enter image description here

I managed to save the range A1:C4 in a variable dynamically in exRange and use the code below to provide a border

 ((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous; 


After

enter image description here

+2
source
 // ** - 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))); 
0
source

This code places a border around the area from (row1, col1) to (row2, col2). Individual cells do not receive borders. The color of a variable is the integer number of the color index. See http://www.databison.com/excel-color-palette-and-color-index-change-using-vba/ for a list of index numbers and corresponding colors.

  Range cell1 = worksheet.Cells[row1,col1]; Range cell2 = worksheet.Cells[row2,col2]; Range range = worksheet.get_Range(cell1,cell2); range.BorderAround( Type.Missing, XlBorderWeight.xlThick, (XlColorIndex)color, Type.Missing ); 
0
source
 ws.UsedRange.BorderAround( Xl.XlLineStyle.xlDash, Xl.XlBorderWeight.xlThick, Xl.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.Blue)); 
0
source

Here is my solution, just use the UsedRange () function

 Excel.Range tRange = oSheet.UsedRange; tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; tRange.Borders.Weight = Excel.XlBorderWeight.xlThin; 
0
source

Source: https://habr.com/ru/post/976200/


All Articles