Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel 2000 v Excel 2002 v c ?c m?t m?ng t? m?t ph?m vi trong Excel 2000 v Excel 2002

D?ch tiu ? D?ch tiu ?
ID c?a bi: 186122 - 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 ch?ng t? lm th? no ? t? ?ng ho Microsoft Excel v tr? v? cc gi tr? t? m?t ph?m vi multi-cell m?t m?ng.

THNG TIN THM

? tr? v? cc gi tr? t? m?t ph?m vi multi-cell m khng c?n cu cc t? bo ci m?t, b?n ph?i s? d?ng ch?c nng thnh vin GetValue c?a cc T?m bay ?i t?ng ? tr? v? cc gi tr? m?t bi?n th?. B?n sau xy d?ng m?t SAFEARRAY d?a trn cc bi?n th? b?n tr? l?i t? GetValue. M?t khi b?n ? SAFEARRAY, b?n c th? truy c?p vo cc y?u t? s? d?ng GetElement. Cc b?c sau y minh h?a ny x? l? v ch?ng minh lm th? no b?n c th? truy c?p c? hai chu?i v s? cc y?u t?.

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? vi?c 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, nh?n s? bi vi?t d?i y ? xem cc bi vi?t 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

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

  1. Trong Microsoft Excel, t?o ra m?t b?ng tnh m?i. C cc t? bo A1:C8 v?i m?t h?n h?p c?a d? li?u chu?i v s?. Lu b?ng tnh nh "c:\test.xls" v thot kh?i Microsoft Excel.
  2. Lm theo b?c 1 qua 12 trong bi vi?t sau y trong cc C s? ki?n th?c Microsoft ? 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
  3. ? pha trn c?a cc t?p tin AutoProjectDlg.cpp, thm cc d?ng sau:
          #include "excel8.h"
    					
  4. Thm m? sau y ? CAutoProjectDlg::OnRun() trong cc AutoProjectDlg.cpp t?p tin:

    M?u m?

          // OLE Variant for Optional.
          COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
          _Application objApp;
          _Workbook objBook;
          Workbooks objBooks;
          Worksheets objSheets;
          _Worksheet objSheet;
          Range objRange;
          VARIANT ret;
    
          // Instantiate Excel and open an existing workbook.
          objApp.CreateDispatch("Excel.Application");
          objBooks = objApp.GetWorkbooks();
          objBook = objBooks.Open("C:\\Test.xls",
                    VOptional, VOptional, VOptional, VOptional,
                    VOptional, VOptional, VOptional, VOptional,
                    VOptional, VOptional, VOptional, VOptional);
          objSheets = objBook.GetWorksheets();
          objSheet = objSheets.GetItem(COleVariant((short)1));
    
          //Get the range object for which you wish to retrieve the
          //data and then retrieve the data (as a variant array, ret).
          objRange = objSheet.GetRange(COleVariant("A1"), COleVariant("C8"));
          ret = objRange.GetValue();
    
          //Create the SAFEARRAY from the VARIANT ret.
          COleSafeArray sa(ret);
    
          //Determine the array's dimensions.
          long lNumRows;
          long lNumCols;
          sa.GetUBound(1, &lNumRows);
          sa.GetUBound(2, &lNumCols);
    
          //Display the elements in the SAFEARRAY.
          long index[2];
          VARIANT val;
          int r, c;
          TRACE("Contents of SafeArray\n");
          TRACE("=====================\n\t");
          for(c=1;c<=lNumCols;c++)
          {
             TRACE("\t\tCol %d", c);
          }
          TRACE("\n");
          for(r=1;r<=lNumRows;r++)
          {
             TRACE("Row %d", r);
             for(c=1;c<=lNumCols;c++)
             {
                index[0]=r;
                index[1]=c;
                sa.GetElement(index, &val);
                switch(val.vt)
                {
                case VT_R8:
                   {
                      TRACE("\t\t%1.2f", val.dblVal);
                      break;
                   }
                case VT_BSTR:
                   {
                      TRACE("\t\t%s",(CString)val.bstrVal);
                      break;
                   }
    
                case VT_EMPTY:
                   {
                      TRACE("\t\t<empty>");
                      break;
                   }
                }
             }
             TRACE("\n");
          }
    
          //Close the workbook without saving changes
          //and quit Microsoft Excel.
          objBook.Close(COleVariant((short)FALSE), VOptional, VOptional);
          objApp.Quit();
    					
  5. D? n bin d?ch v ch?y n.
  6. Ch?n Run. Cc gi tr? trong cc t? bo A1:C8 ?c hi?n th? trong cc ?u ra c?a s?.

THAM KH?O

? c thm thng tin v? t? ?ng ho Microsoft Excel b?ng cch s? d?ng MFC, xin vui l?ng b?m vo nh?ng con s? bi vi?t d?i y ? xem cc bi vi?t trong cc C s? ki?n th?c Microsoft:
186120 Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v i?n vo m?t t?m ho?t ?ng v?i m?t m?ng
184663 Lm th? no ? nhng v t? ?ng ho m?t Microsoft Excel Worksheet v?i MFC
179706 Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v t?o/?nh d?ng m?t Workbook m?i
178781HOWTO: T? ?ng b?ng cch s? d?ng MFC v ch?c nng b?ng tnh Excel
178783HOWTO: S? d?ng MFC ? t?o ra m?t Microsoft Excel bi?u ?
167668Lm th? no ? v?t qua m?t SafeArray dy trong m?t phin b?n *

Thu?c tnh

ID c?a bi: 186122 - 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 Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
T? kha:
kbprogramming kbfunctions kbautomation kbhowto kbinterop kbmt KB186122 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:186122

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