Cell의 색상(배경색) 정하기.

 

생성되는 Cntreior.h파일을 기존 폴더에 복사합니다.

  - MFC에서 Excel 컨트롤 하기(1)에서 만든 파일들이 있는 곳.

1)"Cnterior.h"의 

 //#import "C:\\Program Files\\Microsoft Office\\Office16\\EXCEL.EXE" no_namespace//<<------주석처리

 

2) "ExcelCtrl.h"파일에  #include "Cnterior.h"//<<------링크 추가.

 

3) 사용 방법

void CExcelCtrl::SetValue(int nSheet, CString strCellPos, CString strNewValue){} 함수에 추가.

CRange range의 범위 내의 Cell을 칠하게 된다.

Cnterior Itr;
Itr = range.get_Interior();
Itr.put_Color(COleVariant((double)BColor));

 

4) 함수 Overriding을 통해 Color인자를 받아 색상을 지정한다.

 

주: Data 쓰기 전에 색상변경을 먼저 해주어야 한다.

    - 순서가 바뀌지 않으면 Try Catch에서 예외가 발생(랜덤)한다.

void CExcelCtrl::SetValue(int nSheet, CString strCellPos, CString strNewValue)
{
	if (nSheet < 1 || m_app == nullptr)
		return;

	try
	{
		// sheet 생성, 연결 (1번 시트)
		CWorksheet sheet;
		sheet = m_worksheets.get_Item(COleVariant((short)nSheet));
		sheet.Activate();
		// range 생성, 연결
		CRange range;
		range.AttachDispatch(sheet.get_Cells(), true);

		range = sheet.get_Range(COleVariant(strCellPos.GetBuffer(strCellPos.GetAllocLength())), m_covOptional);
		range.Select();

		//------------------------------------------------------
		//색상 변경
		COLORREF BColor = RGB(255, 0, 0);
		Cnterior Itr;
		Itr = range.get_Interior();
		Itr.put_Color(COleVariant((double)BColor));
		//------------------------------------------------------
		//Data 쓰기.
		range.put_Value2(COleVariant(strNewValue));
		//------------------------------------------------------
		range.ReleaseDispatch();
		sheet.ReleaseDispatch();
	}
	catch (CMemoryException* e)
	{
		CMemoryException* ep = e;
		AfxMessageBox(L"CMemoryException Could not clean up workbook.");
	}
	catch (CFileException* e)
	{
		CFileException* pe = e;
		AfxMessageBox(L"CFileException Could not clean up workbook.");
	}
	catch (CException* e)
	{
		CException* pe = e;
		AfxMessageBox(L"CException Could not clean up workbook.");
	}
}

 

Cnterior.h
0.00MB

 

+ Recent posts