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

D?ch tiu ? D?ch tiu ?
ID c?a bi: 186120 - 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 ch?ng t? lm th? no ? t? ?ng ho Microsoft Excel v i?n vo m?t Multi-Cell t?m ho?t ?ng v?i m?t m?ng c?a cc gi tr?.


? i?n vo m?t ph?m vi multi-cell m khng populating cc t? bo-m?t, b?n ph?i t?o ra m?t phin b?n hai chi?u SAFEARRAY m b?n v?t qua ? Excel b?i ch?c nng SetValue ku ?i t?ng ph?m vi. Cc b?c sau minh h?a cho qu tr?nh ny.

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? cc 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 hm bao l?p cho Excel 2000 (Excel9.olb) ho?c Excel 2002 (Excel.olb). Cho 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 th vi?n 2002 ki?u, h?y b?m vo cc s? bi vi?t d?i y ? xem n trong 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. Lm theo b?c 1 ?n 12 trong bi vi?t sau y trong Microsoft Ki?n th?c c s? ? 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:
    178749 Lm 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. T?o h?p tho?i ra trong b?c 4 v 5 c?a bi bo ph? huynh 178749, thm cc i?u khi?n thu?n v?i thu?c tnh theo quy ?nh. C?ng thm cc bi?n thnh vin tng ?ng:
                                      Member                Member
          Control   Name              Variable Type         Variable Name
          Edit      IDC_STARTINGCELL  m_sStartingCell       CString
          Edit      IDC_NUMROWS       m_iNumRows            short
          Edit      IDC_NUMCOLS       m_iNumCols            short
          CheckBox  IDC_STRING        m_bFillWithStrings    BOOL
  3. ? pha trn c?a cc t?p tin AutoProjectDlg.cpp, thm 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 range;
          // Instantiate Excel and start a new workbook.
          objBooks = objApp.GetWorkbooks();
          objBook = objBooks.Add(VOptional);
          objSheets = objBook.GetWorksheets();
          objSheet = objSheets.GetItem(COleVariant((short)1));
          //Get the range where the starting cell has the address
          //m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols.
          range = objSheet.GetRange(COleVariant(m_sStartingCell),
          range = range.GetResize(COleVariant(m_iNumRows),
          //*** Fill the range with an array of values.
          //Create the SAFEARRAY.
          COleSafeArray saRet;
          DWORD numElements[2];
          numElements[0]= m_iNumRows;   //Number of rows in the range.
          numElements[1]= m_iNumCols;   //Number of columns in the range.
             saRet.Create(VT_BSTR, 2, numElements);
             saRet.Create(VT_R8, 2, numElements);
          //Fill the SAFEARRAY.
          long index[2];
          long iRow;
          long iCol;
                index[0] = iRow;
                index[1] = iCol;
                if(m_bFillWithStrings)      //Fill with Strings.
                   VARIANT v;
                   CString s;
                   v.vt = VT_BSTR;
                   s.Format("r%dc%d", iRow, iCol);
                   v.bstrVal = s.AllocSysString();
                   saRet.PutElement(index, v.bstrVal);
                else                     //Fill with Numbers.
                   double d;
                   d = (iRow*1000) + iCol;
                   saRet.PutElement(index, &d);
          //Set the range value to the SAFEARRAY.
          //Return control of Excel to the user.
  5. Bin d?ch v ch?y cc d? n.
  6. Ch? r? cc gi tr? sau cho cc i?u khi?n trn h?p tho?i:
          Control               Contents
          IDC_STARTINGCELL      A1
          IDC_NUMROWS           10
          IDC_NUMCOLS           5
          IDC_STRING            True
    Bm OK.

    K?t qu?: M?t b?ng tnh m?i ?c t?o ra v cc t? bo A1:E10 ?u tin b?ng tnh c dn c v?i cc gi tr? chu?i.
  7. Ch? r? cc gi tr? sau cho cc i?u khi?n trn h?p tho?i:
          Control               Contents
          IDC_STARTINGCELL      C3
          IDC_NUMROWS           2
          IDC_NUMCOLS           9
          IDC_STRING            False
    Bm OK.

    K?t qu?: M?t b?ng tnh m?i ?c t?o ra v cc t? bo C3:K4 ?u tin b?ng tnh c dn c v?i cc gi tr? s?.


? bi?t thm chi ti?t v? t? ?ng ho Microsoft Excel s? d?ng MFC, xin vui l?ng xem cc bi vi?t sau trong c s? ki?n th?c Microsoft:
186122 Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v c ?c m?t m?ng t? m?t ph?m vi

184663 Lm th? no ? nhng v t? ?ng ho m?t Microsoft Excel Worksheet v?i MFC

179706 S? d?ng MFC ? t? ?ng ho Excel v t?o/?nh d?ng m?t Workbook m?i

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

178783 Lm th? no ? s? d?ng MFC ? t?o ra m?t Microsoft Excel bi?u ?

Thu?c tnh

ID c?a bi: 186120 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.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:
kbautomation kbcode kbhowto kbinterop kbmt KB186120 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:186120

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