Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v t?o v ?nh d?ng m?t b?ng tnh m?i

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

? Trang ny

TM T?T

Bi vi?t ny cho th?y lm th? no ? t? ?ng b?ng cch s? d?ng Microsoft Excel th vi?n Microsoft Foundation Class (MFC), phin b?n 4,2 (ci ?t v?i Microsoft Visual C++ b?n 5.0 v 6.0).

Bi vi?t m t? m?t k? thu?t cho vi?c s? d?ng t? ?ng ha OLE ? t?o/?nh d?ng m?t Microsoft Excel workbook; n gi?i thi?u m?t s? phng php/ti s?n (t? lo?i Microsoft Excel th vi?n) cho thm d? li?u vo b?ng tnh v ?nh d?ng cc b?ng tnh.

Ghi ch cho t? ?ng ho Microsoft Excel 2000 v 2002

M?u m? trong bi vi?t ny s? d?ng hm bao l?p ?c t?o ra t? Excel 97 ?i t?ng th vi?n (Excel 8.olb). V?i cht thay ?i, i?u ny M? c th? ?c p d?ng cho m?t khch hng t? ?ng ha s? d?ng l?p hm bao cho Excel 2000 (Excel9.olb) ho?c Excel 2002 (Excel.olb). ? c thm thng tin v? b?ng cch s? d?ng m?u m? ?c m t? trong bi vi?t ny v?i Microsoft Excel 2000 ho?c 2002 lo?i th vi?n, xin vui l?ng b?m vo s? bi vi?t d?i y ? xem n trong cc C s? ki?n th?c Microsoft:
224925 Thng tin: Th vi?n ki?u cho cc vn ph?ng c th? thay ?i v?i m?i pht hnh

THNG TIN THM

B?n c th? sao chp m? trong bi vi?t ny ? thng bo x? l? ch?c nng c?a m?t s? ki?n ?c xc ?nh trong t?p tin .cpp MFC. Tuy nhin, m?c ch cc m? l ? minh h?a cho qu tr?nh s? d?ng giao di?n IDispatch v ti kho?n c?a ch?c nng ?c ?nh ngh?a trong th vi?n ki?u Excel8.olb. L?i ch chnh c?a bi vi?t ny, tuy nhin, ?n t? ?c v hi?u bi?t v? m? trong cc V d? ? cc b?n c th? s?a ?i cc v d? ho?c vi?t m? ring c?a b?n ? t? ?ng ha Microsoft Excel 97 s? d?ng MFC.

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

  1. Lm theo b?c 1 ?n 12 t?i Microsoft sau Bi vi?t c s? ki?n th?c ? t?o ra m?t d? n m?u s? d?ng IDispatch giao di?n v ch?c nng thnh vin ?c ?nh ngh?a trong th vi?n ki?u Excel8.olb:
    178749Lm th? no ? t?o t? ?ng ha d? n s? d?ng MFC v m?t th vi?n ki?u
  2. ? pha trn c?a cc t?p tin AutoProjectDlg.cpp, thm cc d?ng sau:
          #include "excel8.h"
    					
  3. Thm m? sau y ? CAutoProjectDlg::OnRun() trong cc AutoProjectDLG.cpp t?p tin:

    M?u m?

          // 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.SetValue(COleVariant("First Name"));
          range = sheet.GetRange(COleVariant("B1"),COleVariant("B1"));
          range.SetValue(COleVariant("Last Name"));
          range = sheet.GetRange(COleVariant("C1"),COleVariant("C1"));
          range.SetValue(COleVariant("Full Name"));
          range = sheet.GetRange(COleVariant("D1"),COleVariant("D1"));
          range.SetValue(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.SetValue(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);
    					
  4. Thm cc ch?c nng sau y ? AutoProjectDLG.cpp, ?nh v? n m?t ni no tr?c khi CAutoProjectDlg::OnRun():

    M?u m?

          void FillSafeArray(OLECHAR FAR* sz, int iRow, int iCol,
                             COleSafeArray* sa)
          {
    
             VARIANT v;
             long index[2];
    
             index[0] = iRow;
             index[1] = iCol;
    
             VariantInit(&v);
             v.vt = VT_BSTR;
             v.bstrVal = SysAllocString(sz);
             sa->PutElement(index, v.bstrVal);
             SysFreeString(v.bstrVal);
             VariantClear(&v);
    
          }
    					
  5. T?i ph?ng thu nh pht tri?n, ch?y cc ?ng d?ng. Khi h?p tho?i h?p xu?t hi?n (v d? t?o ra m?t h?p tho?i d?a trn ?ng d?ng), b?m ch?y nt.

Thu?c tnh

ID c?a bi: 179706 - L?n xem xt sau cng: 18 Thang Chin 2011 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Foundation Class Library 4.2
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
T? kha:
kbnewfile kbfunctions kbprogramming kbautomation kbhowto kbinterop kbmt KB179706 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:179706

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