반응형

1. Point

  • 엑셀 셀 스타일을 변수로 만들어 원하는 셀에만 적용
  • 셀 색상 넣기
  • 셀 테두리 설정

 

 

반응형

 

 

2. 전체 코드

// 참조 추가
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;

namespace manual_NPOI_excel_IO
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var newFile = @"./dir/newbook.xlsx";

            using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
            {

                IWorkbook workbook = new XSSFWorkbook();
                // 시트 생성
                ISheet sheet1 = workbook.CreateSheet("Sheet1");


                // 셀에 적용할 스타일 생성
                var style1 = workbook.CreateCellStyle();
                style1.FillForegroundColor = HSSFColor.Blue.Index2;
                style1.FillPattern = FillPattern.SolidForeground;
                style1.Alignment = HorizontalAlignment.Center;
                style1.BorderBottom = BorderStyle.Medium;
                style1.BorderRight = BorderStyle.DashDotDot;

                // 스타일 적용할 셀 선택
                var cell2 = sheet1.CreateRow(0).CreateCell(0);
                cell2.CellStyle = style1;
                cell2.SetCellValue(0);


                // 작업 내용 파일에 작성 및 저장
                workbook.Write(fs);
            }
        }
    }
}
반응형

+ Recent posts