Post List

2015년 1월 4일 일요일

C++ xls (Excel 파일) 제어 라이브러리

C++에서 Excel File을 작성하는 방법이 있어서 소개하고자 한다.

첨부에 있는 File 4개만 Project에 추가하면 사용이 가능하다.

각 Class별 사용법은 아래 그림을 참조하면 이해가 쉽다.



1. BasicExcel 의 객체를 이용하여 BasicExcelWorkSheet XLSFormatManager 를 생성한다.
2. BasicExcelWorkSheet의 객체에서 Column, Row Index를 이용하여 BasicExcelCell 객체를 얻어온다.
3. ExcelFont 객체와 XLSFormatManager 객체를 사용하여 CellFormat 객체를 생성한다.
4. BasicExcelCell 객체에 Text 값과 CellFormat 객체를 넣어주면 끝.

먼저 예제부터 보자.

#include "ExcelFormat.h"
using namespace ExcelFormat;

int main(int argc, _TCHAR* argv[])
{
 BasicExcel xls;
 xls.New(1);
 BasicExcelWorksheet* sheet = xls.GetWorksheet(0);
 XLSFormatManager fmt_mgr(xls);

 ExcelFont font_bold;
 font_bold._weight = FW_BOLD;

 CellFormat fmt_bold(fmt_mgr);
 fmt_bold.set_font(font_bold);

 int col, row = 0;
 for (col = 0; col < 10; col++)
 {
  BasicExcelCell* cell = sheet->Cell(row, col);

  cell->Set("TITLE");
  cell->SetFormat(fmt_bold);
 }

 while(++row < 4)
 {
  for (int col = 0; col < 10; col++)
   sheet->Cell(row,col)->Set("Text");
 }

 row++;

 ExcelFont font_red_bold;
 font_red_bold._weight = FW_BOLD;
 font_red_bold._color_index = EGA_RED;

 CellFormat fmt_red_bold(fmt_mgr, font_red_bold);
 fmt_red_bold.set_color1(COLOR1_PAT_SOLID);
 fmt_red_bold.set_color2(MAKE_COLOR2(EGA_BLUE, 0));

 CellFormat fmt_green(fmt_mgr, ExcelFont().set_color_index(EGA_GREEN));

 for (col = 0; col < 10; col ++)
 {
  BasicExcelCell* cell = sheet->Cell(row, col);

  cell->Set("xxx");
  cell->SetFormat(fmt_red_bold);

  cell = sheet->Cell(row, ++col);
  cell->Set("yyy");
  cell->SetFormat(fmt_green);
 }

 xls.SaveAs("a.xls");

 return 0;
}


위의 Code를 실행하면 Excel 파일이 하나 생성된다.


좀 더 상세한 내용은 아래 Link의 글을 참조하길 바란다.

http://www.codeproject.com/Articles/42504/ExcelFormat-Library




Font, Underline 등에 대한 예제도 있다.

첨부 : ExcelFormat.zip

댓글 없음:

댓글 쓰기