Làm th? nào ð? s? d?ng MFC ð? t?o ra m?t Microsoft Excel bi?u ð?

ID c?a bài: 178783 - Xem s?n ph?m mà bài này áp d?ng vào.
Bung t?t c? | Thu g?n t?t c?

? Trang này

TÓM T?T

Bài vi?t này th?o lu?n v? cách s? d?ng phiên b?n 4,2 c?a Microsoft Foundation Thý vi?n Class (MFC) ðý?c cài ð?t v?i Microsoft Visual C++ Phiên b?n 5.0 và 6.0 ð? t? ð?ng hoá Microsoft Excel ð? cho nó s? cý m?t b?ng v?i d? li?u và t?o charts.

THÔNG TIN THÊM

B?n có th? sao chép m? trong bài vi?t này ð? ch?c nãng x? l? thý c?a m?t s? ki?n ðý?c xác ð?nh trong t?p tin .cpp MFC. Tuy nhiên, m?c ðích c?a m? là minh h?a cho quá tr?nh s? d?ng giao di?n IDispatch và các thành viên ch?c nãng ðý?c ð?nh ngh?a trong thý vi?n ki?u Excel. L?i ích chính ð?n t? ð?c và hi?u bi?t v? các m? do ðó b?n có th? s?a ð?i các ví d?, ho?c vi?t m? t? ð?u ð? t? ð?ng hoá Microsoft Excel 97, Excel 2000 ho?c Excel 2002.

Ghi chú cho t? ð?ng hoá Microsoft Excel 2000 và sau ðó:

M?t s? phýõng pháp và các thu?c tính ð? thay ð?i trong Microsoft Excel 2000 và m?i hõn. Ð? thêm thông tin v? vi?c s? d?ng m?u m? ðý?c mô t? trong bài vi?t này v?i Microsoft Excel 2000 và thý vi?n ki?u sau này, h?y xem bài vi?t sau trong cõ s? ki?n th?c Microsoft:
224925 Thông tin: Thý vi?n ki?u cho các vãn ph?ng có th? thay ð?i v?i m?i phát hành

Các bý?c ð? t?o ra các d? án

  1. Trong Microsoft Excel, t?o ra m?t b?ng tính m?i tên là Test.xls và lýu nó vào thý m?c g?c c?a ? C.
  2. Làm theo bý?c 1 ð?n 12 trong sau ðây Microsoft Knowledge Base bài vi?t ð? t?o ra m?t d? án m?u s? d?ng giao di?n IDispatch và ch?c nãng thành viên ðý?c ð?nh ngh?a trong thý vi?n ki?u Excel:
    178749 Làm th? nào ð? t?o ra m?t d? án t? ð?ng hóa b?ng cách s? d?ng MFC và m?t thý vi?n ki?u
  3. ? phía trên c?a AutoProjectDlg.cpp, thêm d?ng sau:
          #include "excel8.h" // excel9.h for Excel 2000, excel.h for Excel 2002
    					
  4. Thêm m? sau ðây ð? CAutoProjectDlg::OnRun() trong các AutoProjectDLG.cpp t?p tin.

    M?u m?

          try
          {
           _Application app;  // app is the Excel _Application object.
           _Workbook book;
           _Worksheet sheet;
           _Chart chart;
    
           Workbooks books;
           Worksheets sheets;
           Range range;
           ChartObjects chartobjects;
           Charts charts;
           LPDISPATCH lpDisp;
    
           // Common OLE variants. These are easy variants to use for
           // calling arguments.
           COleVariant
                      covTrue((short)TRUE),
                      covFalse((short)FALSE),
                      covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
           // Start Excel and get the Application object.
           if(!app.CreateDispatch("Excel.Application"))
           {
            AfxMessageBox(
                 "Couldn't start Excel and get an application 0bject");
            return;
           }
    
           // Set visible.
           app.SetVisible(TRUE);
    
           // Get Workbooks collection.
           lpDisp = app.GetWorkbooks();  // Get an IDispatch pointer.
           ASSERT(lpDisp);
           books.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                            // to the books object.
              // Open a workbook.
              lpDisp = books.Open("C:\\Test",
                          covOptional, covOptional, covOptional, covOptional,
                          covOptional, covOptional, covOptional, covOptional,
                          covOptional, covOptional, covOptional, covOptional,
                          covOptional, covOptional, covOptional); // Excel 2000 requires only 13 arguments
              ASSERT(lpDisp);  // It should have worked.
    
           // Attach to a Workbook object.
           book.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                           // to the Workbook object.
    
           // Get sheets.
           lpDisp = book.GetSheets();
           ASSERT(lpDisp);
           sheets.AttachDispatch(lpDisp);
    
           lpDisp = sheets.GetItem( COleVariant((short)(1)) );
           ASSERT(lpDisp);
    
           // Attach the lpDisp pointer to a Worksheet object.
           sheet.AttachDispatch(lpDisp);
    
           lpDisp = sheet.GetRange(COleVariant("A1"), COleVariant("W40"));
                                   // The range is from A1 to W40.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // to the range object.
           range.Clear();  // Could be ClearContents().
    
           ::Sleep(500); // So you can see it happen.
    
           lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("A3"));
                                                     // From A3 to A3.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // the range object.
           range.SetValue(COleVariant("March")); // Excel 97 & Excel 2000.
    
    range.SetValue2(COleVariant("March")); // Insert March into range.
    
    
           // Following is a series of repetitive steps to populate the
           // worksheet's cells with a series of Months and values to be
           // used in the Chart object, which is yet to be constructed.
    
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("B3"));
    
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("12")); // 97 & 2000
    
    range.SetValue2(COleVariant("12")); //  Value for March.
    
    
           lpDisp = sheet.GetRange(COleVariant("A4"), COleVariant("A4"));
                // Months will be in column A, values in column B.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("April"));// Excel 97 & Excel 2000
    
    range.SetValue2(COleVariant("April")); // Excel 2002
    
    
           lpDisp = sheet.GetRange(COleVariant("B4"), COleVariant("B4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("8")); // Excel 97 & Excel 2000
    
    range.SetValue2(COleVariant("8")); // Excel 2002
    
           lpDisp = sheet.GetRange(COleVariant("A5"), COleVariant("A5"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("May"));
    
    range.SetValue2(COleVariant("May"));
    
           lpDisp = sheet.GetRange(COleVariant("B5"), COleVariant("B5"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("2"));
    
    range.SetValue2(COleVariant("2"));
    
           lpDisp = sheet.GetRange(COleVariant("A6"), COleVariant("A6"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("June"));
    
    range.SetValue2(COleVariant("June"));
    
           lpDisp = sheet.GetRange(COleVariant("B6"), COleVariant("B6"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("11"));
    
    range.SetValue2(COleVariant("11"));
    
           lpDisp = sheet.GetRange(COleVariant("A7"), COleVariant("A7"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("July"));
    
    range.SetValue2(COleVariant("July"));
    
           lpDisp = sheet.GetRange(COleVariant("B7"), COleVariant("B7"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("16"));
    
    range.SetValue2(COleVariant("16"));
    
           // The cells are populated. To start the chart,
           // declare some long variables and site the chart.
           long left, top, width, height;
           left = 100;
           top = 10;
           width = 350;
           height = 250;
    
           lpDisp = sheet.ChartObjects(covOptional);
    
           ASSERT(lpDisp);
           chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
                                      // for ChartObjects to the chartobjects
                                      // object.
           ChartObject chartobject = chartobjects.Add(left, top, width, height); 
                                    //defines the rectangle, 
                                    // adds a new chart at that rectangle and 
                                    // assigns its object reference to a 
                                    // ChartObject variable named chartobject
           chart.AttachDispatch(chartobject.GetChart()); // GetChart() returns
                                             // LPDISPATCH, and this attaches 
                                             // it to your chart object.
    
           lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("B7"));
                             // The range containing the data to be charted.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
    
           VARIANT var; // ChartWizard needs a Variant for the Source range.
           var.vt = VT_DISPATCH; // .vt is the usable member of the tagVARIANT
                                 // Struct. Its value is a union of options.
           var.pdispVal = lpDisp; // Assign IDispatch pointer
                                  // of the Source range to var.
    
           chart.ChartWizard(var,                    // Source.
                            COleVariant((short)11),  // Gallery: 3d Column.
                            covOptional,             // Format, use default.
                            COleVariant((short)1),   // PlotBy: xlRows.
                            COleVariant((short)0),   // CategoryLabels.
                            COleVariant((short)1),   // SeriesLabels.
                            COleVariant((short)TRUE),  // HasLegend.
                            COleVariant("Use by Month"),  // Title.
                            COleVariant("Month"),    // CategoryTitle.
                            COleVariant("Usage in Thousands"),  // ValueTitles.
                            covOptional              // ExtraTitle.
                            );
           // The return is void.
           ::Sleep(3000);
           chartobject.Delete();  // Removes the first chartobject, sets the
           // ChartObjects.Item() count to 0. The next chart will restore the
           // item count to 1.
           ::Sleep(3000);  // Set the selected range to be erased.
           range.Clear();  // Erase the usage data.
    
           // Beginning of chart 2.
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("B3"));
                                          // From B3 to B3.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // to the range object.
           range.SetValue(COleVariant("Chocolate")); // Insert Chocolate into
                                                     // the range object.
    range.SetValue2(COleVariant("Chocolate")); // Insert Chocolate
    
           // Following is a series of repetitive steps to populate the
           // worksheet's cells with a series of Flavors and values to be
           // used in the chart object, your second chart.
    
           lpDisp = sheet.GetRange(COleVariant("B4"), COleVariant("B4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("12")); // Value for Chocolate.
    
    range.SetValue2(COleVariant("12")); // Value for Chocolate.
    
           lpDisp = sheet.GetRange(COleVariant("C3"), COleVariant("C3"));
                // Flavors will be in row 3, values in row 4.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("Vanilla"));
    
    range.SetValue2(COleVariant("Vanilla"));
    
           lpDisp = sheet.GetRange(COleVariant("C4"), COleVariant("C4"));
    
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("8"));
    
    range.SetValue2(COleVariant("8"));
    
           lpDisp = sheet.GetRange(COleVariant("D3"), COleVariant("D3"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("Orange"));
    
    range.SetValue2(COleVariant("Orange"));
    
           lpDisp = sheet.GetRange(COleVariant("D4"), COleVariant("D4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("6"));
    
    range.SetValue2(COleVariant("6"));
    
           // The cells are populated. To start the chart,
           // define the bounds, and site the chart.
    
           left = 250;
           top = 40;
           width = 300;
    
           height = 300;
    
           lpDisp = sheet.ChartObjects(covOptional);
    
           ASSERT(lpDisp);
           chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
                                       // for ChartObjects to the chartobjects
                                       // object.
           chartobjects.Add(left, top, width, height); // Adds 1 to item count.
    
           //**************************************
           lpDisp = chartobjects.Item( COleVariant((short)(1)) );  // It was
               // zero, but just added one at a new location,
               // with new left, top, width, and height.
           ASSERT(lpDisp);
           chartobject.AttachDispatch(lpDisp); // Use definition of new chart
                                               // site.
           chart.AttachDispatch(chartobject.GetChart());
           //**************************************
    
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("D4"));
                             // Range containing the data to be charted.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
    
           var.pdispVal = lpDisp; // Contains IDispatch pointer
                                  // to the Source range.
    
           chart.ChartWizard(var,                    // Source.
                            COleVariant((short)11),  // Gallery = 3D Column.
                            covOptional,             // Format, use default.
                            COleVariant((short)2),   // PlotBy xlColumns.
                            COleVariant((short)0),   // CategoryLabels.
                            COleVariant((short)1),   // SeriesLabels.
                            COleVariant((short)TRUE),  // HasLegend.
                            COleVariant("Use by Flavor"),  // Title.
                            COleVariant("Flavor"),    // CategoryTitle.
                            COleVariant("Usage in Barrells"),  // ValueTitles.
                            covOptional              // ExtraTitle.
                            );
           // The return is void.
           ::Sleep(3000);
    
           //Show the chart in Print Preview.
           chart.PrintOut(COleVariant((short)1),     // From (page #).
                          COleVariant((short)1),     // To (page #).
                          COleVariant((short)1),     // Copies.
                          COleVariant((short)TRUE),  // Preview.
                          covOptional,               // ActivePrinter.
                          covFalse,                  // PrintToFile.
                          covFalse                   // Collate.
                          covOptional                // PrToFileName // 2002 only
                          );
    
           book.SetSaved(TRUE); // Avoids the 'Save changes?' dialog box.
           app.Quit(); // Excel departs.
    
           // By default, the pointer references for the objects
           // range, book, chart, chartobjects, sheet, and app
           // are automatically released when they go out of scope.
           // ReleaseDispatch()s are unnecessary.
    
           ::Sleep(1000);
           AfxMessageBox("Just executed App.Quit()");
    
          }  // End of processing logic.
    
          catch(COleException *e)
          {
            char buf[1024];
    
            sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
            ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
          }
    
          catch(COleDispatchException *e)
          {
           char buf[1024];
           sprintf(buf,
                   "COleDispatchException. SCODE: %08lx, Description: \"%s\".",
                   (long)e->m_wCode,
                   (LPSTR)e->m_strDescription.GetBuffer(1024));
           ::MessageBox(NULL, buf, "COleDispatchException",
                        MB_SETFOREGROUND | MB_OK);
          }
    
          catch(...)
          {
           ::MessageBox(NULL, "General Exception caught.", "Catch-All",
                        MB_SETFOREGROUND | MB_OK);
          }
    					
  5. B?n có th? c?n ph?i thay ð?i m? trong CAutoProjectDlg::OnRun() ð? ch? ra ðý?ng d?n chính xác cho b?ng tính c?a b?n Test.xls. The workbook tham chi?u trong d?ng sau:
          lpDisp = books.open("C:\\Test", . . .);
    					

THAM KH?O

Ð? có thêm thông tin v? các ?ng d?ng t? ð?ng hóa c?a vãn ph?ng, b?m vào s? bài vi?t dý?i ðây ð? xem bài vi?t trong cõ s? ki?n th?c Microsoft:
222101 Làm th? nào ð? t?m và s? d?ng vãn ph?ng ð?i tý?ng m?u tài li?u

Thu?c tính

ID c?a bài: 178783 - L?n xem xét sau cùng: 19 Thaìng Taìm 2011 - Xem xét l?i: 2.0
Áp d?ng
  • Microsoft Foundation Class Library 4.2, khi ðý?c dùng v?i:
    • Microsoft Excel 97 Standard Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
  • Microsoft Excel 2002 Standard Edition
T? khóa: 
kbautomation kbhowto kbinterop kbmt KB178783 KbMtvi
Máy d?ch
QUAN TROòNG: Bài vi?t này ðý?c d?ch b?ng ph?n m?m d?ch máy c?a Microsoft ch? không ph?i do con ngý?i d?ch. Microsoft cung c?p các bài vi?t do con ngý?i d?ch và c? các bài vi?t do máy d?ch ð? b?n có th? truy c?p vào t?t c? các bài vi?t trong Cõ s? Ki?n th?c c?a chúng tôi b?ng ngôn ng? c?a b?n. Tuy nhiên, bài vi?t do máy d?ch không ph?i lúc nào c?ng hoàn h?o. Lo?i bài vi?t này có th? ch?a các sai sót v? t? v?ng, cú pháp ho?c ng? pháp, gi?ng nhý m?t ngý?i ný?c ngoài có th? m?c sai sót khi nói ngôn ng? c?a b?n. Microsoft không ch?u trách nhi?m v? b?t k? s? thi?u chính xác, sai sót ho?c thi?t h?i nào do vi?c d?ch sai n?i dung ho?c do ho?t ð?ng s? d?ng c?a khách hàng gây ra. Microsoft c?ng thý?ng xuyên c?p nh?t ph?n m?m d?ch máy này.
Nh?p chu?t vào ðây ð? xem b?n ti?ng Anh c?a bài vi?t này:178783

Cung câìp PhaÒn hôÌi