[VC]如何import excel.application使用ole automation

有關於C/C++的語法, 程式等
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

[VC]如何import excel.application使用ole automation

文章 tim »

使用方法
http://support.microsoft.com/default.as ... -US;179706

建立 header 檔的方法.
http://support.microsoft.com/default.as ... -US;178749


Application Type Library
--------------------------------------------------

Microsoft Access 97 Msacc8.olb
Microsoft Jet Database 3.5 DAO350.dll
Microsoft Binder 97 Msbdr8.olb
Microsoft Excel 97 Excel8.olb
Microsoft Graph 97 Graph8.olb
Microsoft Office 97 Mso97.dll
Microsoft Outlook 97 Msoutl97.olb
Microsoft PowerPoint 97 Msppt8.olb

Microsoft Word 97 Msword8.olb
Microsoft Access 2000 Msacc9.olb
Microsoft Jet Database 3.51 DAO360.dll
Microsoft Binder 2000 Msbdr9.olb
Microsoft Excel 2000 Excel9.olb
Microsoft Graph 2000 Graph9.olb
Microsoft Office 2000 Mso9.dll
Microsoft Outlook 2000 Msoutl9.olb
Microsoft PowerPoint 2000 Msppt9.olb
Microsoft Word 2000 Msword9.olb

Microsoft Access 2002 Msacc.olb
Microsoft Excel 2002 Excel.exe
Microsoft Graph 2002 Graph.exe
Microsoft Office 2002 MSO.dll
Microsoft Outlook 2002 MSOutl.olb
Microsoft PowerPoint 2002 MSPpt.olb
Microsoft Word 2002 MSWord.olb

Microsoft Office Access 2003 Msacc.olb
Microsoft Office Excel 2003 Excel.exe
Microsoft Graph 2003 Graph.exe
Microsoft Office 2003 MSO.dll
Microsoft Office Outlook 2003 MSOutl.olb
Microsoft Office PowerPoint 2003 MSPpt.olb
Microsoft Office Word 2003 MSWord.olb



附件為程式原始檔
其中的 excel 為使用 excel xp (10), excel.exe import 進來的 excel.h, excel.cpp

這是其中實作的某一個 function.

代碼: 選擇全部

void CTestdiDlg::OnButton3()
{
    // Commonly used OLE variants.
     COleVariant
                covTrue((short)TRUE),
                covFalse((short)FALSE),
                covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);


     _Application app;
     Workbooks books;
     _Workbook book;

     Worksheets sheets;
     _Worksheet sheet;
     Range range;
     Font font;
     Range cols;

     // Start Excel and get Application object.

     if(!app.CreateDispatch("Excel.Application"))
     {
      AfxMessageBox("Couldn't start Excel and get Application object.");
      return;
     }


     //Get a new workbook.
     books = app.GetWorkbooks();
     book = books.Add (covOptional);

     //Get the first sheet.
     sheets =book.GetSheets();
     sheet = sheets.GetItem(COleVariant((short)1));

     //Fill cells A1, B1, C1, and D1 one cell at a time with "headers".
     range = sheet.GetRange(COleVariant("A1"),COleVariant("A1"));

     range.Setvalue2(COleVariant("First Name"));
     range = sheet.GetRange(COleVariant("B1"),COleVariant("B1"));
     range.Setvalue2(COleVariant("Last Name"));
     range = sheet.GetRange(COleVariant("C1"),COleVariant("C1"));
     range.Setvalue2(COleVariant("Full Name"));
     range = sheet.GetRange(COleVariant("D1"),COleVariant("D1"));
     range.Setvalue2(COleVariant("Salary"));


     //format A1:D1 as bold, vertical alignment = center.
     range = sheet.GetRange(COleVariant("A1"), COleVariant("D1"));
     font = range.GetFont();
     font.SetBold(covTrue);
     range.SetVerticalAlignment(
              COleVariant((short)-4108));   //xlVAlignCenter = -4108

     //Fill A2:B6 with an array of values (First & Last Names).
     {
        COleSafeArray saRet;
        DWORD numElements[]={5,2};   //5x2 element array
        saRet.Create(VT_BSTR, 2, numElements);


        //Fill the 5x2 SafeArray with the following data:
        //   John      Smith
        //   Tom       Brown
        //   Sue       Thomas
        //   Jane      Jones
        //   Adam      Johnson

 

       
FillSafeArray(L"John", 0, 0, &saRet);
        FillSafeArray(L"Smith", 0, 1, &saRet);
        FillSafeArray(L"Tom", 1, 0, &saRet);
        FillSafeArray(L"Brown", 1, 1, &saRet);
        FillSafeArray(L"Sue", 2, 0, &saRet);
        FillSafeArray(L"Thomas", 2, 1, &saRet);

        FillSafeArray(L"Jane", 3, 0, &saRet);
        FillSafeArray(L"Jones", 3, 1, &saRet);
        FillSafeArray(L"Adam", 4, 0, &saRet);
        FillSafeArray(L"Johnson", 4, 1, &saRet);

        range = sheet.GetRange(COleVariant("A2"), COleVariant("B6"));
        range.Setvalue2(COleVariant(saRet));

        saRet.Detach();
     }

     //Fill C2:C6 with a relative formula (=A2 & " " & B2).
     range = sheet.GetRange(COleVariant("C2"), COleVariant("C6"));
     range.Setformula(COleVariant("=A2 & " " & B2"));

     //Fill D2:D6 with a formula(=RAND()*100000) and apply a number
     //format.
     range = sheet.GetRange(COleVariant("D2"), COleVariant("D6"));
     range.Setformula(COleVariant("=RAND()*100000"));
     range.SetNumberformat(COleVariant("$0.00"));

     //AutoFit columns A:D.
     range = sheet.GetRange(COleVariant("A1"), COleVariant("D1"));
     cols = range.GetEntireColumn();
     cols.AutoFit();

     //Manipulate a variable number of columns for Quarterly Sales Data.
     {
        short NumQtrs;
        CString msg;
        Range resizedrange;
        Interior interior;
        Borders borders;

        //Determine how many quarters to display data for.
        for(NumQtrs=1;NumQtrs<=3;NumQtrs++)
        {
           msg.format("Enter sales data for %d quarter(s)?", NumQtrs);
           if(AfxMessageBox(msg,MB_YESNO)==IDYES)
           {
              break;
           }
        }
        msg.format("Displaying data for %d quarters.", NumQtrs);
        AfxMessageBox(msg);

        //Starting at E1, fill headers for the number of columns selected.
        range = sheet.GetRange(COleVariant("E1"), COleVariant("E1"));
        resizedrange = range.GetResize(covOptional, COleVariant(NumQtrs));
        resizedrange.Setformula(
               COleVariant("="Q" & COLUMN()-4 & CHAR(10) & "Sales""));
        //Change the Orientation and WrapText properties for the headers.
        resizedrange.SetOrientation(COleVariant((short)38));
        resizedrange.SetWrapText(covTrue);
        //Fill the interior color of the headers.
        interior = resizedrange.GetInterior();
        interior.SetColorIndex(COleVariant((short)36));

        //Fill the columns with a formula and apply a number format.
        range = sheet.GetRange(COleVariant("E2"), COleVariant("E6"));
        resizedrange = range.GetResize(covOptional, COleVariant(NumQtrs));
        resizedrange.Setformula(COleVariant("=RAND()*100"));
        resizedrange.SetNumberformat(COleVariant("$0.00"));

        //Apply borders to the Sales data and headers.
        range = sheet.GetRange(COleVariant("E1"), COleVariant("E6"));
        resizedrange= range.GetResize(covOptional, COleVariant(NumQtrs));
        borders = resizedrange.GetBorders();
        borders.SetWeight(COleVariant((short)2));   //xlThin = 2

        //Add a Totals formula for the Quarterly sales data and apply a
        //border.
        range = sheet.GetRange(COleVariant("E8"), COleVariant("E8"));
        resizedrange = range.GetResize(covOptional, COleVariant(NumQtrs));
        resizedrange.Setformula(COleVariant("=SUM(E2:E6)"));
        borders = resizedrange.GetBorders();
        {
           Border bottomborder;
           bottomborder = borders.GetItem((long)9);
           bottomborder.SetLinestyle(
                          COleVariant((short)-4119));   //xlDouble = -4119
           bottomborder.SetWeight(
                          COleVariant((short)4));       //xlThick = 4

        }
     }

     //Make the application visible and give the user control of
     //Microsoft Excel.
     app.SetVisible(TRUE);
     app.SetUserControl(TRUE);
}
附加檔案
testdi.part02.rar
(79.79 KiB) 已下載 367 次
testdi.part01.rar
(250 KiB) 已下載 327 次
多多留言, 整理文章, 把經驗累積下來.....
回覆文章