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.");
}
}
'작업 > MFC' 카테고리의 다른 글
비주얼 스튜디오 if문 for문 확장 축소 (문 블록 개요) (0) | 2021.10.28 |
---|---|
CFileDialog Error (File Click-Mouse R) (1) | 2021.10.14 |
MFC OpneMP 간단 사용하기. (1) | 2020.03.10 |
MFC Tip : CTime 시간 설정, TRACE() 찍기 오류 상황. (0) | 2020.03.10 |
MFC에서 Excel 컨트롤 하기(2) (3) | 2020.01.23 |