programing

C#을 사용하여 Excel의 여러 셀 주위에 테두리를 설정하는 방법

elecom 2023. 8. 21. 19:35
반응형

C#을 사용하여 Excel의 여러 셀 주위에 테두리를 설정하는 방법

저는 엑셀 파일을 만드는 프로젝트를 하고 있습니다.

엑셀 파일을 구성하기 위해 여러 셀에 테두리를 두르는 데 어려움을 겪고 있습니다.

예를 들어, B5 셀에서 B10 셀까지의 경계를 원한다고 가정해 보겠습니다.B5, B6, B7 사이에 경계선이 있어서는 안 됩니다.

현재 다음 코드가 있습니다.

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

경계를 만들지만 모든 셀에 대해 하나의 큰 경계 대신 모든 셀 주위에 경계를 배치합니다.

어떻게 하면 이 일을 해낼 수 있을까요?

개별적으로 설정해야 합니다.

.Borders[Excel.XlBordersIndex.xlEdgeBottom] 
.Borders[Excel.XlBordersIndex.xlEdgeRight]
.Borders[Excel.XlBordersIndex.xlEdgeLeft]  
.Borders[Excel.XlBordersIndex.xlEdgeTop]

이것이 도움이 될 수도 있습니다.

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

저는 성능에 영향을 주지 않고 이 작업을 수행했습니다.포맷하기 위해 간단한 엑셀을 사용합니다.

전에

enter image description here

범위를 저장할 수 있었습니다.A1:C4exRange에서 동적으로 변수에 포함되며 아래 코드를 사용하여 경계를 지정했습니다.

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


끝나고

enter image description here

다음은 각 셀 주위에 테두리를 설정하는 코드입니다.

xlWS.get_Range("C9", "N9").Cells.Borders.Weight = XL.XlBorderWeight.xlMedium;
// ** - 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)));        

여기 내 솔루션이 있습니다. 간단히 UsedRange() 함수를 사용하십시오.

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

이 코드는 (1행, col1)에서 (2행, col2)까지의 영역 주위에 테두리를 두릅니다.개별 셀에는 테두리가 없습니다.변수 색상은 정수 색상 색인 번호입니다.인덱스 번호 및 해당 색상 목록은 http://www.databison.com/excel-color-palette-and-color-index-change-using-vba/ 을 참조하십시오.

    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 );
ws.UsedRange.BorderAround(
                        Xl.XlLineStyle.xlDash,
                        Xl.XlBorderWeight.xlThick,
                        Xl.XlColorIndex.xlColorIndexAutomatic,
                        ColorTranslator.ToOle(Color.Blue));

언급URL : https://stackoverflow.com/questions/11743809/how-can-a-border-be-set-around-multiple-cells-in-excel-using-c-sharp

반응형