Lm th? no ? nhng v t? ?ng ho m?t Microsoft Excel b?ng tnh v?i MFC

D?ch tiu ? D?ch tiu ?
ID c?a bi: 184663 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?


Bi vi?t ny m t? cch ? nhng m?t Microsoft Excel B?ng vo m?t ?i t?ng xem trong ?ng d?ng SDI MFC.

Bi vi?t ny bao g?m cc b?c theo b?c h?ng d?n ? nhng b?ng tnh v thm m?t s? vn b?n vo A1, c?ng nh ? ki?n gi?i thch t?ng b?c.

M?c d m?u m? trong bi vi?t ny c th? ?c th?c hi?n v a vo ?ng d?ng c?a b?n, cc l?i ch th?c s? ?n t? ?c v hi?u bi?t v? m?u M?.


Microsoft cung c?p l?p tr?nh v d? ? minh ho? ch?, khng c b?o hnh ho?c th? hi?n hay ng? ?. i?u ny bao g?m, nhng khng gi?i h?n, b?o ?m ng? ? kh? nng bn hng ho?c cho m?t m?c ch c? th?. Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i ngn ng? l?p tr?nh m ang ?c ch?ng minh v v?i nh?ng cng c? ?c s? d?ng ? t?o ra v g? l?i th? t?c. Microsoft h? tr? cc k? s c th? gip gi?i thch cc ch?c nng c?a m?t th? t?c c? th?. Tuy nhin, h? s? s?a ?i cc v d? ? cung c?p thm ch?c nng ho?c xy d?ng quy tr?nh ? p ?ng cc yu c?u c? th? c?a b?n.

? t?o ra cc ?ng d?ng MFC, h?y lm theo cc b?c sau:
  1. S? d?ng AppWizard ? t?o ra m?t m?i MFC AppWizard (EXE) d? n ?c ?t tn "embed_excel."
  2. Ch?n ti li?u duy nh?t nh lo?i ?ng d?ng ? t?o ra, v ch?n Container l lo?i ti li?u h?p ch?t h? tr? ? bao g?m. Ch?p nh?n t?t c? cc thi?t l?p m?c ?nh khc.

    Cc l?p h?c sau y c ?c t?o ra:

    ?ng d?ng: CEmbed_ExcelApp Embed_Excel.h v Embed_Excel.cpp

    Khung: CMainFrame MainFrm.h v MainFrm.cpp

    Ti li?u: CEmbed_ExcelDoc trong Embed_ExcelDoc.h v Embed_ExcelDoc.CPP

    Xem: CEmbed_ExcelView trong Embed_ExcelView.h v Embed_ExcelView.CPP

    Kho ch?a kho?n m?c: CEmbed_ExcelCntrItem CntrItem.h v CntrItem.cpp
  3. Trn cc Xem tr?nh n, nh?p vo ClassWizard. B?m vo cc T? ?ng ha tab, b?m vo Thm Class, v ch?n T? m?t th vi?n ki?u. Xc ?nh v? tr cc Th vi?n lo?i Microsoft Excel, v thm t?t c? cc l?p h?c trong th vi?n ki?u cho d? n c?a b?n. Cho Excel 97, th vi?n ki?u ny t?a l?c t?i Excel8.olb. Cho Excel 2000 lo?i th vi?n n?m ? Excel9.olb, v cho Excel 2002 v sau , th vi?n ki?u n n?m ? Excel.exe.
  4. Thm d?ng sau vo CntrItem.h:
    LPDISPATCH GetIDispatch();
  5. Sau thm cc phng php GetIDispatch ? CntrItem.cpp:
       Sample Code
          *   This method returns the IDispatch* for the application linked to
          *   this container.
          LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch()
             //The this and m_lpObject pointers must be valid for this function
             //to work correctly. The m_lpObject is the IUnknown pointer to
             // this object.
             ASSERT(m_lpObject != NULL);
             LPUNKNOWN lpUnk = m_lpObject;
             //The embedded application must be running in order for the rest
             //of the function to work.
             //QI for the IOleLink interface of m_lpObject.
             LPOLELINK lpOleLink = NULL;
             if (m_lpObject->QueryInterface(IID_IOleLink,
                (LPVOID FAR*)&lpOleLink) == NOERROR)
                ASSERT(lpOleLink != NULL);
                lpUnk = NULL;
                //Retrieve the IUnknown interface to the linked application.
                if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
                   TRACE0("Warning: Link is not connected!\n");
                   return NULL;
                ASSERT(lpUnk != NULL);
             //QI for the IDispatch interface of the linked application.
             LPDISPATCH lpDispatch = NULL;
             if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)
                TRACE0("Warning: does not support IDispatch!\n");
                return NULL;
             //After assuring ourselves it is valid, return the IDispatch
             //interface to the caller.
             ASSERT(lpDispatch != NULL);
             return lpDispatch;
  6. Thm d?ng sau vo Embed_ExcelView.h:
          void EmbedAutomateExcel();
  7. Sau thm cc phng php EmbedAutomateExcel ? Embed_ExcelView.CPP:
    Sample Code
          *   This method encapsulates the process of embedding an Excel
          *   Worksheet in a View object and automating that worksheet to add
          *   some text to cell A1.
          void CEmbed_ExcelView::EmbedAutomateExcel()
             //Change the cursor so the user knows something exciting is going
             CEmbed_ExcelCntrItem* pItem = NULL;
                //Get the document associated with this view, and be sure it's
                CEmbed_ExcelDoc* pDoc = GetDocument();
                //Create a new item associated with this document, and be sure
                //it's valid.
                pItem = new CEmbed_ExcelCntrItem(pDoc);
                // Get Class ID for Excel sheet.
                // This is used in creation.
                CLSID clsid;
                   //Any exception will do. We just need to break out of the
                   //TRY statement.
                // Create the Excel embedded item.
                   //Any exception will do. We just need to break out of the
                   //TRY statement.
                //Make sure the new CContainerItem is valid.
                // Launch the server to edit the item.
                pItem->DoVerb(OLEIVERB_SHOW, this);
                // As an arbitrary user interface design, this sets the
                // selection to the last item inserted.
                m_pSelection = pItem;   // set selection to last inserted item
                //Query for the dispatch pointer for the embedded object. In
                //this case, this is the Excel worksheet.
                LPDISPATCH lpDisp;
                lpDisp = pItem->GetIDispatch();
                //Add text in cell A1 of the embedded Excel sheet
                _Workbook wb;
                Worksheets wsSet;
                _Worksheet ws;
                Range range;
                _Application app;
                //set _Workbook wb to use lpDisp, the IDispatch* of the
                //actual workbook.
                //Then get the worksheet's application.
                app = wb.GetApplication();
                //Then get the first worksheet in the workbook
                wsSet = wb.GetWorksheets();
                ws = wsSet.GetItem(COleVariant((short)1));
                //From there, get a Range object corresponding to cell A1.
                range = ws.GetRange(COleVariant("A1"), COleVariant("A1"));
                //Fill A1 with the string "Hello, World!"
                range.SetValue(COleVariant("Hello, World!"));
               //NOTE: If you are automating Excel 2002, the Range.SetValue method has an 
               //additional optional parameter specifying the data type.  Because the 
               //parameter is optional, existing code will still work correctly, but new 
               //code should use the new convention.  The call for Excel2002 should look 
               //like the following:
               //range.SetValue( C<?xm-insertion_mark_start author="v-thomr" time="20070326T121607-0600"?>O<?xm-insertion_mark_end?><?xm-deletion_mark author="v-thomr" time="20070326T121606-0600" data="o"?>leVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ), 
               //                COleVariant("Hello, World!"));
               //Here, we need to do clean up if something went wrong.
               CATCH(CException, e)
                  if (pItem != NULL)
               //Set the cursor back to normal so the user knows exciting stuff
               //is no longer happening.
  8. Thm d?ng sau vo Embed_ExcelView.h:
          #include "excel8.h"
    NOTE N?u b?n ang t? ?ng ho Excel 2000, t?p tin tiu ? l "excel9.h." N?u b?n ang t? ?ng ho Excel 2002 ho?c m?t phin b?n sau ny c?a Excel, t?p tin tiu ? l "excel.h."
  9. Xem xt cc phng php OnInsertObject() c?a l?p View. N l th v? ? lu ? r?ng phng php ny, v cc phng php m chng ti ? ch? vi?t, n?i b?t tng t?. Trong th?c t?, chng ti ? vi?t m? l ch? n thu?n l m?t tr?ng h?p ?c bi?t c?a OnInsertObject(), cho php ng?i dng ? ch?n t? m?t danh sch c s?n OLE cc ?i t?ng ? chn vo cc ?ng d?ng. B?i v? chng ti ch? mu?n t? ?ng ho cc B?ng tnh Excel, chng ti ghi hnh vi ny. ?i v?i ?ng d?ng c?a chng ti, lo?i b? t?t c? cc M? t? bn trong c?a InsertObject() v thay th? b?ng m?t cu?c g?i ?n EmbedAutomateExcel().
  10. Bin d?ch v ch?y cc ?ng d?ng.
  11. Trn cc Ch?nh s?a tr?nh n, nh?p vo Chn ?i t?ng m?i.
K?t qu?: M?t Microsoft Excel b?ng tnh ?c nhng vo quan i?m. Ngoi ra, cell A1 dn c v?i "Hello, World!" thng qua t? ?ng ha.

Thu?c tnh

ID c?a bi: 184663 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Office Excel 2007
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Foundation Class Library 4.2
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
T? kha:
kbprogramming kbautomation kbhowto kbinterop kbmt KB184663 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:184663

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com