퇴근5분전

 

 이번에 친구 프로그램 만들면서 epplus로 여러 시트를 만들어야 했었다.

그래서 하나 만든게 있다. 엑셀의 특정 시트를 지정해서 읽어서 epplus 구문으로 똑같이 생성하는 코드를 만들어주 주는 소스다.

 

워낙 셀도 많고, 수식도 많이 걸려있고... 그걸 하나 하나 치려니 너무 빡시니까...

 

단, 챠트, 이미지, 등등 객체정보는 대상으로 하지 않았다. 작업 대상 시트엔 없는 내용이었으니...

 

250라인정도 되넹...

 

#파일 오픈.

   using (ExcelPackage package = new ExcelPackage(newTempFile, true))

 

#엑셀에 따로 정의된 스타일 정보. ( xml로 따로 빼서 생성시 넣어주면 스타일 정보가 똑같이 뜬다. )

   string xml = package.Workbook.StylesXml.OuterXml;

 

# 시트 탭 컬러

   @"sheet.TabColor = Color.FromArgb({0}, {1}, {2});",

 

#셀 배경색 넣을때 먼저 지정! ( 없으면 배경색 넣을때 에러! )

   @"sheet.Cells[""{0}""].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.{1};"

#셀 배경색

   @"sheet.Cells[""{0}""].Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml(""#{1}""));"

 

#폰트

   @"sheet.Cells[""{0}""].Style.Font.SetFromFont( new Font(""{1}"", {2}f, FontStyle.{3}));"

 

#폰트색

   @"sheet.Cells[""{0}""].Style.Font.Color.SetColor(ColorTranslator.FromHtml(""#{1}""));"

 

#수식

   @"sheet.Cells[""{0}""].Formula = ""{1}"";"

 

#값

   @"sheet.Cells[""{0}""].Value = {1};"

 

#리치텍스트 ( 셀 안에 컨텐츠에 별도 서식을 지정했을때 value대신 쓴다. )

   @"var excelRich{1} = sheet.Cells[""{0}""].RichText.Add(@""{2}"");"

   @"excelRich{0}.FontName = ""{1}"";"

   @"excelRich{0}.Color = Color.FromArgb({1});"

   @"excelRich{0}.Size = {1};"

   @"excelRich{0}.Bold = {1};"

   @"excelRich{0}.UnderLine = {1};"

   @"sheet.Cells[""{0}""].IsRichText = true;"

 

#숫자포맷 ( 이게 좀 거지 같다.  날짜로 된 컬럼이 숫자로 넘어온다. 다른 숫자와 구분이 안되어 직접 핸들링 해야 함. )

   @"sheet.Cells[""{0}""].Style.Numberformat.Format = @""{1}"";"

 

#셀 병합

   @"sheet.Cells[""{0}""].Merge = {1};"

 

#외곽선 ( 이것도.. 대부분 비슷하게 가져오는데 다른 경우는 만들어진 시트 보면서 직접 변경해줘야 한다. )

   @"sheet.Cells[""{0}""].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.{1};"
   @"sheet.Cells[""{0}""].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.{1};"
   @"sheet.Cells[""{0}""].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.{1};"
   @"sheet.Cells[""{0}""].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.{1};"

 

#셀 가로, 세로 정렬 ( 이것도 다르게 가져오기도 한다. )

   @"sheet.Cells[""{0}""].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.{1};"

   @"sheet.Cells[""{0}""].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.{1};",

 

#컨텐츠가 셀 넘어가지 않게 처리 할때. (=true)

   @"sheet.Cells[""{0}""].Style.WrapText = {1};"

 

#컬럼정보

   @"sheet.Column({0}).BestFit = {1};"
   @"sheet.Column({0}).Collapsed = {1};"
   @"sheet.Column({0}).Width = {1}d;"
   @"sheet.Column({0}).Hidden = {1};"

 

#행정보

   @"sheet.Row({0}).CustomHeight = {1};"
   @"sheet.Row({0}).Collapsed = {1};"
   @"sheet.Row({0}).Height = {1}d;"
   @"sheet.Row({0}).Hidden = {1};"

 

#페이지프린트영역

   @"sheet.PrinterSettings.PrintArea = sheet.Cells[""{0}""];"

 

#정렬인데.. ( 에러날때가 있어..try{}catch{}로 묶어서 사용 )

   @"sheet.PrinterSettings.Orientation = eOrientation.{0};"

 

#페이지정보(기타)

   @"sheet.PrinterSettings.PageOrder = ePageOrder.{0};"
   @"sheet.PrinterSettings.PaperSize = ePaperSize.{0};"
   @"sheet.PrinterSettings.FitToPage = {0};"

   @"sheet.PrinterSettings.FooterMargin = {0}m;"
   @"sheet.PrinterSettings.HeaderMargin = {0}m;"
   @"sheet.PrinterSettings.LeftMargin = {0}m;"
   @"sheet.PrinterSettings.TopMargin = {0}m;"
   @"sheet.PrinterSettings.RightMargin = {0}m;"
   @"sheet.PrinterSettings.BottomMargin = {0}m;"

 

#페이지레이아웃보기

   @"sheet.View.PageBreakView = true;"

#확대비율

   @"sheet.View.ZoomScale = {0};"


 

위 정보를 조합으로 소스를 만들면 휘리릭... 1초만에 소스코드를 얻고 바로 시트작업을 할 수 가 있다.

 

 

epplus는 왜? 저장하고 나서 열면...

 

아무것도 안하고 닫는데, "저장하시겠습니까?" 가 뜰까?

 

저장을 한건데... 왜? 못찾겠다.

 

 

## 중요..

 

 Cell 병합관련해서 삽질을 하던 중...

 

 Merge 되는 셀 정보값은 시작되는 셀에만 값을 넣어야 된다.

 

  Sheent.Cells[ 시작되는 셀 ].Value = 값 또는 .Formula = 수식

 

 시작되는 셀이 아니고 병합되는 전체 셀을 모두 지정하면 각 셀에 모두 값을 넣게 되고

 

이 병합된 셀들을 다른 셀에서 수식으로 연계 할 경우 값이 원하는 값이 나오지 않게 된다.

  

  string.Format( @"sheet.Cells[ ""{0}"" ].Value = {1};",  cell.Start.Address, 100 );