Lm th? no ? t? ?ng ho Excel s? d?ng ch?c nng MFC v b?ng tnh

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

? Trang ny


Bi vi?t ny m t? lm th? no ? t? ?ng ho Microsoft Excel 97, Microsoft Excel 2000, Microsoft Excel 2002 ho?c Microsoft Excel 2003 b?ng cch s? d?ng Microsoft Th vi?n phin b?n 4,2 (ci ?t v?i Microsoft Foundation Class (MFC) Visual C++ b?n 5.0 v 6.0). ?c bi?t, bi vi?t ny minh ho? lm th? no ? s? d?ng ch?c nng b?ng tnh ?c cung c?p b?i m?t add-in ch?ng h?n nh phn tch ToolPak (ATP) v lm th? no ? s? d?ng cc b?ng cng th?c ch?c nng ?c xy d?ng trong Microsoft Excel.


B?n c th? sao chp m? trong bi vi?t ny ? ch?c nng x? l? th c?a m?t s? ki?n ?c xc ?nh trong t?p tin .cpp MFC. Tuy nhin, m?c ch c?a m? l minh h?a cho qu tr?nh s? d?ng IDispatch giao di?n v ch?c nng thnh vin ?c ?nh ngh?a trong Excel8.olb cho Excel 97, trong Excel9.olb cho Excel 2000, v trong Excel.exe cho Excel 2002 v th vi?n ki?u Excel 2003. L?i ch chnh ?n t? ?c v hi?u bi?t v? m? trong v d? ny, do b?n c th? s?a ?i cc v d? ho?c vi?t m? t? ?u ? t? ?ng ho m?t ch?c nng b?ng tnh trong Microsoft Excel s? d?ng MFC.

Cc b?c ? t?o ra cc d? n

  1. Lm theo b?c 1 ?n 12 trong bi vi?t c s? ki?n th?c Microsoft sau y ? t?o ra m?t d? n m?u s? d?ng IDispatch giao di?n v ch?c nng thnh vin quy ?nh t?i Excel8.olb, ho?c Excel9.olb, ho?c Excel.exe cho th vi?n ki?u Excel 2003 v Excel 2002:
    178749Lm th? no ? t?o ra m?t d? n t? ?ng ha b?ng cch s? d?ng MFC v m?t th vi?n ki?u
  2. ? pha trn c?a AutoProjectDlg.cpp, thm d?ng sau:
          #include "excel8.h"
    N?u b?n ang t? ?ng ho Excel 2000, bao g?m excel9.h. N?u b?n ang t? ?ng ho Excel 2002 ho?c Excel 2003, bao g?m excel.h

  3. Thm m? sau y ? CAutoProjectDlg::OnRun() trong cc AutoProjectDLG.cpp t?p tin: M? m?u:
          _Application app;     // app is an _Application object.
          _Workbook book;       // More object declarations.
          _Worksheet sheet;
          Workbooks books;
          Worksheets sheets;
          Range range;          // Used for Microsoft Excel 97 components.
          LPDISPATCH lpDisp;    // Often reused variable.
          // Common OLE variants. Easy variants to use for calling arguments.
            covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
          // Start Microsoft Excel, get _Application object,
          // and attach to app object.
            AfxMessageBox("Couldn't CreateDispatch() for Excel");
          // Set visible.
          // Register the Analysis ToolPak.
          CString sAppPath;
          sAppPath.Format ("%s\\Analysis\\Analys32.xll", app.GetLibraryPath());
            AfxMessageBox("Didn't register the Analys32.xll");
          // Get the Workbooks collection.
          lpDisp = app.GetWorkbooks();     // Get an IDispatch pointer.
          books.AttachDispatch(lpDisp);    // Attach the IDispatch pointer
                                           // to the books object.
          // Open a new workbook and attach that IDispatch pointer to the
          // Workbook object.
          lpDisp = books.Add( covOptional );
          book.AttachDispatch( lpDisp );
             // To open an existing workbook, you need to provide all
             // arguments for the Open member function. In the case of 
             // Excel 2002 you must provide 16 arguments.
             // However in Excel 2003 you must provide 15 arguments.
             // The code below opens a workbook and adds it to the Workbook's
             // Collection object. It shows 13 arguments, required for Excel
             // 2000.
             // You need to modify the path and file name for your own
             // workbook.
          // lpDisp = books.Open("C:\\Test",     // Test.xls is a workbook.
          // covOptional, covOptional, covOptional, covOptional, covOptional,
          // covOptional, covOptional, covOptional, covOptional, covOptional,
          // covOptional, covOptional );   // Return Workbook's IDispatch
          // pointer.
          // Get the Sheets collection and attach the IDispatch pointer to your
          // sheets object.
          lpDisp = book.GetSheets();
          // Get sheet #1 and attach the IDispatch pointer to your sheet
          // object.
          lpDisp = sheets.GetItem( COleVariant((short)(1)) );
                                            //GetItem(const VARIANT &index)
          // Fill range A1 with "1/25/98", the settlement date.
          lpDisp = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));
          range.SetValue(COleVariant("1/25/98")); // Excel 97 & Excel 2000
    range.SetValue2(COleVariant("1/25/98")); // Excel 2002 and Excel 2003
          // Fill range A2 with "11/15/99", the maturity date.
          lpDisp = sheet.GetRange(COleVariant("A2"), COleVariant("A2"));
          range.SetValue(COleVariant("11/15/99"));  // Excel 97 & Excel 2000
    range.SetValue2(COleVariant("11/15/99")); // Excel 2002 and Excel 2003
          // Fill range A3 with "2", the frequency for semi-annual interest
          // payments.
          lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("A3"));
          range.SetValue(COleVariant("2"));  // Excel 97 & Excel 2000
    range.SetValue2(COleVariant("2"));  // Excel 2002 and Excel 2003
          // Fill range A4 with 1, the basis (actual/actual).
          lpDisp = sheet.GetRange(COleVariant("A4"), COleVariant("A4"));
          range.SetValue(COleVariant("1")); // Excel 97 & Excel 2000
    range.SetValue2(COleVariant("1")); // Excel 2002 and  Excel 2003
          // Fill range C1 with the formula "=COUPNCD(A1, A2, A3, A4)" and
          // format the cell with a Date type of the Number format.
          lpDisp = sheet.GetRange(COleVariant("C1"), COleVariant("C1"));
          range.SetFormula(COleVariant("=COUPNCD(A1, A2, A3, A4)"));
          /* This is an alternative that works without placing variables on
          // the worksheet.
          // The values are arguments contained in the SetFormula() call.
          // range.SetFormula(COleVariant(
          // *** The example in this block uses a built-in Microsoft Excel
          // function.
          // You do not have to register any add-in to use the built-in
          // Microsoft Excel worksheet functions.
          lpDisp = sheet.GetRange(COleVariant("C3"), COleVariant("C3"));
          range.SetFormula(COleVariant("=SUM(A3, A4)"));
          // or use:
          // range.SetFormula(COleVariant("=SUM(2,1)"));
          // *** End of example for built-in function usage.
          // Release dispatch pointers.
          // This is not really necessary because
          // the default second parameter of AttachDispatch releases
          // when the current scope is lost.
          } // End of processing.
            catch(COleException *e)
            char buf[1024];     // For the Try...Catch error message.
            sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
            ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
          catch(COleDispatchException *e)
            char buf[1024];     // For the Try...Catch error message.
                   "COleDispatchException. SCODE: %08lx, Description: \"%s\".",
            ::MessageBox(NULL, buf, "COleDispatchException",
                               MB_SETFOREGROUND | MB_OK);
            ::MessageBox(NULL, "General Exception caught.", "Catch-All",
                               MB_SETFOREGROUND | MB_OK);


? c thm thng tin v? cc ?ng d?ng t? ?ng ha c?a vn ph?ng, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
222101 Lm th? no ?: T?m v s? d?ng vn ph?ng ?i t?ng m?u ti li?u

Thu?c tnh

ID c?a bi: 178781 - L?n xem xt sau cng: 19 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Foundation Class Library 4.2, khi ?c dng v?i:
    • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 2002 Standard Edition
T? kha:
kbautomation kbhowto kbinterop kbmt KB178781 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:178781

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