|
존재하는 화일을 열려고 하면 xlBooks.OleProcedure("Open", "testAC.xls"); 에서 예외 오류가
발생합니다. 가르쳐 주셔요.....
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant xlApp;
if (xlApp.IsEmpty())
{//Variant로 엑셀 인스턴스를 만든다.
xlApp = Variant::CreateObject("Excel.Application");
ShowMessage("New Object");
}
// 엑셀이 보이게끔 한다.
xlApp.OlePropertySet("Visible", true);
// 엑셀의 워크북 오브젝트 접근
Variant xlBooks;
xlBooks = xlApp.OlePropertyGet("Workbooks");
// 존재하는 엑셀 파일을 연다.
xlBooks.OleProcedure("Open", "testAC.xls");
// 만약에 만들 경우는 xlBooks.OleProcedure("Add");
// 어플리케이션 내에서 첫번째 워크북을 얻는다.
Variant xlBook = xlBooks.OlePropertyGet("Item", 1);
// 워크시트들을 관리할 수 있는 오브젝트를 얻는다.
Variant xlSheets = xlBook.OlePropertyGet("Worksheets");
// 첫번째 워크시트를 얻는다.
Variant xlSheet = xlSheets.OlePropertyGet("Item", 1);
// 셀로 작업할 경우
Variant VRange;
VRange = xlSheet.OlePropertyGet("Range", "A10");
VRange.OlePropertySet("Value", "CellA10");
VRange = xlSheet.OlePropertyGet("Range", "A11");
VRange.OlePropertySet("Value", "CellA11");
VRange = xlSheet.OlePropertyGet("Range", "A12");
VRange.OlePropertySet("Value", "CellA12");
VRange = xlSheet.OlePropertyGet("Range", "B10");
VRange.OlePropertySet("Value", "CellB10");
VRange = xlSheet.OlePropertyGet("Range", "B11");
VRange.OlePropertySet("Value", "CellB11");
VRange = xlSheet.OlePropertyGet("Range", "B12");
VRange.OlePropertySet("Value", "CellB12");
// 2*2 셀을 지운다.
VRange = xlSheet.OlePropertyGet("Range", "A10:B10");
VRange.OleFunction("ClearContents");
// 값을 계산할 때
xlSheet = xlApp.OlePropertyGet("ActiveSheet");
for (int i = 1; i <= 8; i++)
xlSheet.OlePropertySet("cells", 1, i, i);
xlSheet.OlePropertyGet("Range", "A2:H9").OlePropertySet("FormulaR1C1", "=R[-1]C*2");
// 빠져나갈 때
xlBooks.OleProcedure("Close");
if (::MessageBox(0, " 엑셀을 빠져나가시겠습니까?", "Test",
MB_YESNO | MB_ICONASTERISK) == IDYES)
xlApp.OleProcedure("Quit");
}
|