Lm th? no ? xy d?ng m?t Add-in (XLL) cho Excel s? d?ng Visual c ++

D?ch tiu ? D?ch tiu ?
ID c?a bi: 178474 - 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

XLL m?t l m?t add-in cho Microsoft Excel m b?n c th? xy d?ng v?i b?t k? tr?nh bin d?ch h? tr? xy d?ng DLLs (nng ?ng lin k?t th vi?n). Bi vi?t ny ?c thi?t k? ? gip b?n b?t ?u xy d?ng XLLs v?i Microsoft Visual C++. ? lm theo cc b?c ?c nu trong bi vi?t ny, b?n nn c m?t s? kinh nghi?m xy d?ng DLLs v b?n c?n ph?i c c?a Microsoft Excel 97 Developer Kit, m ch?a t?p tin tiu ? v th vi?n c?n thi?t ? xy d?ng m?t XLL.

THNG TIN THM

Cc b?c ? t?o ra m?t XLL

  1. T?o m?t d? n MFC AppWizard (.dll) m?i ?c g?i l Anewxll.
  2. Sao chp Xlcall.h, Framewrk.h, Framewrk.c v Xlcall32.lib t? cc Microsoft Excel 97 Developer Kit th m?c d? n c?a b?n.
  3. ?i tn Framewrk.c ? Framewrk.cpp.
  4. Thm Framewrk.cpp vo t?p tin ngu?n d? n c?a b?n.
  5. Thm sau # bao g?m d?ng vo pha trn c?a Framewrk.cpp ? trnh tr?nh bin d?ch l?i v? c tiu ?:
    
          #include "stdafx.h"
    					
  6. Thay ?i d?ng sau trong Framewrk.cpp:
          #include "..\..\include\xlcall.h"
    					
    ?:
          #include "xlcall.h"
    					
  7. Nh?p vo ci ?t trn menu d? n. Nh?p vo tab lin k?t v thm Xlcall32.lib cho cc module ?i t?ng/th vi?n ch?nh s?a h?p.
  8. Thm sau # bao g?m cc bo co t? ?u c?a Anewxll.cpp:
          #include "xlcall.h"
          #include "framewrk.h"
    					
    Lu ?: Cc d? n l by gi? thi?t l?p m?t cch thch h?p v nn bin d?ch, Tuy nhin, b?n c khng ?c h? tr? XLL nu ra. Cung c?p cho cc b?c c?n l?i m?t s? m?u m?, b?n c th? thm vo XLL ? gip b?n b?t ?u.

  9. N?i thm m? sau y ? Anewxll.cpp: M?u m? -----------
          //================================================================
          // Commonly used global variables
          int err;
          char buf[8192];
          char txt[8192];
    
          // Function registering table
          int nFuncs;
    
          // proc, type_text, function_text, arg, macro_type, category,
          // shortcut_text, help_topic, function_help
          static LPSTR func[][9] = {
          {" MyFunc", " JJJ", " MyFunc", " ", " 1", " MyCat", " ", " ", " "},
          {" MyMotd", " I", " MyMotd", " ", " 1", " MyCat", " ", " ", " "},
          {0,0,0,0, 0, 0, 0}
          };
    
          // Menu table
          int nMenuItems;
    
          static LPSTR menu[][5] = {
          {" &MyMenu", " ", " ", " Joe's Xll menu!!!", " "},
          {" M.O.T.D."," MyMotd", " ", " Message of the Day!", " "},
          {0, 0, 0, 0, 0}
          };
    
          // Initialization routine
          BOOL __stdcall xlAutoOpen(void) {
             AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
    
             // DEBUG output to indicate when called
             AfxMessageBox("xlAutoOpen() called!", MB_SETFOREGROUND);
    
             int i, j;
    
             // Get XLL file name
             static XLOPER xDll;
             Excel(xlGetName, &xDll, 0);
    
             // Prefix strengths with their length & count items
             // Note the framework's TempStr() function prefixes the
             // lengths anyway, but this is for other code that might
             // use the arrays
             for(nFuncs=0;     func[nFuncs][0];     nFuncs++) {
                 for(i=0; i<9; i++) {
                     func[nFuncs][i][0]     = (BYTE) strlen(func[nFuncs][i]+1);
                 }
    
             }
    
             for(nMenuItems=0; menu[nMenuItems][0]; nMenuItems++) {
                 for(i=0; i<5; i++) {
                 menu[nMenuItems][i][0] = (BYTE) strlen(menu[nMenuItems][i]+1);
                 }
             }
    
             // Loop through the function list, and register the functions
             for(i=0; i<nFuncs; i++) {
    
                // Register a function
                err = Excel(xlfRegister, 0, 9, (LPXLOPER)&xDll,
                   (LPXLOPER)TempStr(func[i][0]),
                   (LPXLOPER)TempStr(func[i][1]),
                   (LPXLOPER)TempStr(func[i][2]),
                   (LPXLOPER)TempStr(func[i][3]),
                   (LPXLOPER)TempStr(func[i][4]),
                   (LPXLOPER)TempStr(func[i][5]),
                   (LPXLOPER)TempStr(func[i][6]),
                   (LPXLOPER)TempStr(func[i][7]),
                   (LPXLOPER)TempStr(func[i][8])
                   );
    
                if(err != xlretSuccess) {
                 sprintf(buf, "xlfRegister for function %d, err = %d", i, err);
                 AfxMessageBox(buf, MB_SETFOREGROUND);
                }
             }
    
             // Free XLL file name from the xlGetName call made earlier
             Excel(xlFree, 0, 1, (LPXLOPER)&xDll);
    
             // Menu support section
             static XLOPER xMenu;
             static XLOPER xMenuList[10*5];
             ASSERT(nMenuItems< 10);
    
             // Build menu
             xMenu.xltype            = xltypeMulti;
             xMenu.val.array.lparray = &xMenuList[0];
             xMenu.val.array.rows    = nMenuItems;
             xMenu.val.array.columns = 5;
    
             for(i=0; i<nMenuItems; i++) {
                 for(j=0; j<5; j++) {
                     xMenuList[j+i*5].xltype  = xltypeStr;
                     xMenuList[j+i*5].val.str = menu[i][j];
                 }
             }
    
             // Add menu
            Excel(xlfAddMenu,0,3,TempNum(1),(LPXLOPER)&xMenu,TempStr(" Help"));
    
             // Finished
             return 1;
          }
    
          // Cleanup routine
          BOOL __stdcall xlAutoClose(void) {
             ::MessageBox(NULL, "xlAutoClose()", "Debug", MB_SETFOREGROUND );
    
             // Delete menu
             Excel(xlfDeleteMenu, 0, 2, TempNum(1), TempStr(" MyMenu"));
    
             return 1;
          }
    
          // Support for descriptive information about the add-in(s)
          // You can add a new customized title for the user, but
          // unfortunately, only an add-in written in Microsoft Visual Basic
          // can add a description string.
          LPXLOPER _stdcall xlAddInManagerInfo(LPXLOPER xAction) {
             static XLOPER xInfo, xIntAction;
    
             // Find out what action must be taken
             Excel(xlCoerce, &xIntAction, 2, xAction, TempInt(xltypeInt));
    
             // DEBUG output to indicate when called
             sprintf(buf, "xlAddInManagerInfo(%ld)", (long)xIntAction.val.w);
             ::MessageBox(NULL, "xlAddInManagerInfo()", "Debug",
                 MB_SETFOREGROUND );
    
             // Set title if asked
             if(xIntAction.val.w == 1) {
                 xInfo.xltype = xltypeStr;
                 xInfo.val.str = " My Add-in!!!!";
                 xInfo.val.str[0] = (char)strlen(&xInfo.val.str[1]);
             }
             else {
                 xInfo.xltype = xltypeErr;
                 xInfo.val.err = xlerrValue;
             }
    
             return (LPXLOPER)&xInfo;
          }
    
            short __stdcall MyMotd(void) {
             char *name[] = {
                "Rebekah",
                "Brent",
                "John",
                "Joseph",
                "Robert",
                "Sara",
                0
             };
             char *quote[] = {
                "An apple a day, keeps the doctor away!",
                "Carpe Diem: Seize the Day!",
                "What you dare to dream, dare to do!",
                "I think, therefore I am.",
                "A place for everything, and everything in its place.",
                "Home is where the heart is.",
    
                0
             };
    
             int nNames, nQuotes;
    
             for(nNames=0; name[nNames]; nNames++);
             for(nQuotes=0; quote[nQuotes]; nQuotes++);
    
             sprintf(buf, "%s says '%s'", name[rand()%nNames],
                quote[rand()%nQuotes]);
             ::MessageBox(NULL, buf, "XLL MOTD", MB_SETFOREGROUND );
    
             return 0;
          }
    
          // Example function that returns the product of its two parameters
          long __stdcall MyFunc(long parm1, long parm2) {
           sprintf(buf, "You sent %ld and %ld to MyFunc()!", parm1, parm2);
           ::MessageBox(NULL, buf, "MyFunc() in Anewxll!!!", MB_SETFOREGROUND);
    
           return parm1 * parm2;
          }
          //=================================================================
    					
  10. Thm d?ng sau vo cu?i t?p tin Anewxll.def c?a b?n:
          xlAutoOpen
          xlAutoClose
          xlAddInManagerInfo
          MyMotd
          MyFunc
    					
  11. B?n by gi? c th? bin d?ch d? n c?a b?n ? s?n xu?t m?t DLL ?c g?i l Anewxll.dll. M?t khi DLL ?c bin so?n, ?i tn n ? Anewxll.xll.

B?ng cch s? d?ng ti?n ch trong v?i Microsoft Excel

  1. B?t ?u m?t b?ng tnh m?i trong Microsoft Excel.
  2. Nh?p vo Add-ins trn tr?nh n cng c?. Duy?t thm Anewxll.xll v nh?n OK. Nh?n th?y r?ng khi b?n b?m OK trong h?p tho?i Add-ins, xlAutoOpen ch?c nng ch?y.
  3. Nh?p M.O.T.D vo tr?nh n MyMenu. Khi m?c tr?nh n c?a b?n ?c nh?p, ch?c nng MyMotd ch?y ? hi?n th? m?t MessageBox v?i m?t bo nh "Rebekah ni ' m?t qu? to m?t ngy, gi? cc bc s? i!'".
  4. Trong m?t t? bo, thm cng th?c sau y:
    =MYFUNC(2,6)
    					
    MYFUNC tr? v? 12, s?n ph?m th? 2 v 6.

  5. Nh?p vo Add-ins trn tr?nh n cng c?. B? ch?n thm trong h?p ki?m v b?m Ok. Nh?n th?y r?ng khi b?n b?m OK trong h?p tho?i Add-ins, xlAutoClose ch?c nng ch?y.

Thm ghi ch

N?u b?n ang s? d?ng Microsoft Visual C++ 6.0, v d? tr?c khng thnh cng khi b?n bin d?ch n trong ch? ? g? l?i. S? th?t b?i x?y ra b?i v? nh c?a Visual C++ 6.0, chu?i literals ?c ?t trong ?c b? nh? ch? trong g? l?i xy d?ng, v b quy?t XLL cc nh pht tri?n s? d?ng ? ti?n t? chi?u di dy c?a h? gy ra m?t s? vi ph?m truy c?p. Nh m?t cng vi?c-xung quanh, b?n c th? h?y b? /ZI tr?nh bin d?ch chuy?n trong thi?t l?p d? n c?a b?n, ho?c ch? c?n ki?m tra phin b?n xy d?ng. ? bi?t thm chi ti?t, xem bi vi?t sau trong c s? ki?n th?c Microsoft:
198477 PRB: S? d?ng /ZI c th? gy ra s? vi ph?m truy c?p

THAM KH?O

? bi?t thm chi ti?t lin quan ?n XLLs, xin vui l?ng xem:
Microsoft Excel 97 Developer Kit (ISBN: 1-57231-498-2)

Thu?c tnh

ID c?a bi: 178474 - L?n xem xt sau cng: 19 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
T? kha:
kbhowto kbmt KB178474 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:178474

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