COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant VTrue((short)TRUE);
COleVariant VFalse((short)FALSE);
TRY
{
//-----------------------------------------------------*
// app 엑셀 오브젝트 생성
//-----------------------------------------------------*
if(!app.CreateDispatch("Excel.Application")) // 엑셀 실행.
{
AfxMessageBox("Could not start EXCEL!");
return;
}
app.SetVisible(true); // 화면 디스플레이.
//-----------------------------------------------------*
// Get Workbooks
//-----------------------------------------------------*
LPDISPATCH lpdisp;
lpdisp=app.GetWorkbooks(); // 파일 단위..
ASSERT(lpdisp);
books.AttachDispatch(lpdisp);
//-----------------------------------------------------*
// FileOpen
//-----------------------------------------------------*
/* // 파일을 오픈할 경우.
books.Open( "c:\\Master.xls", VOptional, VFalse,
VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional,
VTrue, VOptional, VOptional,
VFalse, VOptional, VOptional);
*/
//-----------------------------------------------------*
// Get WorkBook
//-----------------------------------------------------*
lpdisp=books.Add(VOptional); //새로운 엑셀객체를 생성하였을 경우
//lpdisp=books.Get_Default(COleVariant((short)(1))); //FileOpen()일 경우
ASSERT(lpdisp);
book.AttachDispatch(lpdisp);
//-----------------------------------------------------*
// Get WorkSheets
//-----------------------------------------------------*
lpdisp=book.GetSheets(); // 북에서 Sheets 를 얻어옴.
ASSERT(lpdisp);
sheets.AttachDispatch(lpdisp);
//-----------------------------------------------------*
// Get WorkSheet
//-----------------------------------------------------*
//lpdisp=sheets.GetItem(COleVariant((short)(1))); // Sheets에서 해당 Sheet 순서로 얻어오기.
//lpdisp=sheets.GetItem(COleVariant("Index")); // Sheets에서 해당 Sheet 명으로 얻어오기
//ASSERT(lpdisp);
//sheet.AttachDispatch(lpdisp);
lpdisp=sheets.Add(VOptional, VOptional, COleVariant((short)(3)), VOptional ); // Sheet 3개 추가
ASSERT(lpdisp);
lpdisp=sheets.GetItem(COleVariant((short)(1))); // Sheet 명 부여하기
ASSERT(lpdisp);
sheet.AttachDispatch(lpdisp);
sheet.SetName("UserIndex");
lpdisp=sheets.GetItem(COleVariant((short)(2)));
ASSERT(lpdisp);
sheet.AttachDispatch(lpdisp);
sheet.SetName("Peak");
lpdisp=sheets.GetItem(COleVariant((short)(3)));
ASSERT(lpdisp);
sheet.AttachDispatch(lpdisp);
sheet.SetName("Distance");
}
CATCH(CException, e)
{
book.Close(VFalse, VOptional, VOptional);
app.Quit();
app.ReleaseDispatch();
}
END_CATCH
출처 : http://justinoh.tistory.com/category/?page=12