XLL m?t là m?t add-in cho Microsoft Excel mà b?n có th? xây d?ng v?i b?t k?
tr?nh biên d?ch h? tr? xây d?ng DLLs (nãng ð?ng liên k?t thý vi?n). Bài vi?t này
ðý?c thi?t k? ð? giúp b?n b?t ð?u xây d?ng XLLs v?i Microsoft Visual C++. Ð?
làm theo các bý?c ðý?c nêu trong bài vi?t này, b?n nên có m?t s? kinh nghi?m
xây d?ng DLLs và b?n c?n ph?i có c?a Microsoft Excel 97 Developer Kit,
mà ch?a t?p tin tiêu ð? và thý vi?n c?n thi?t ð? xây d?ng m?t XLL.
T?o m?t d? án MFC AppWizard (.dll) m?i ðý?c g?i là Anewxll.
Sao chép Xlcall.h, Framewrk.h, Framewrk.c và Xlcall32.lib t? các
Microsoft Excel 97 Developer Kit thý m?c d? án c?a b?n.
Ð?i tên Framewrk.c ð? Framewrk.cpp.
Thêm Framewrk.cpp vào t?p tin ngu?n d? án c?a b?n.
Thêm sau # bao g?m d?ng vào phía trên c?a Framewrk.cpp ð? tránh
tr?nh biên d?ch l?i v? có tiêu ð?:
#include "stdafx.h"
Thay ð?i d?ng sau trong Framewrk.cpp:
#include "..\..\include\xlcall.h"
ð?:
#include "xlcall.h"
Nh?p vào cài ð?t trên menu d? án. Nh?p vào tab liên k?t và thêm
Xlcall32.lib cho các module ð?i tý?ng/thý vi?n ch?nh s?a h?p.
Thêm sau # bao g?m các báo cáo t? ð?u c?a Anewxll.cpp:
#include "xlcall.h"
#include "framewrk.h"
Lýu ?: Các d? án là bây gi? thi?t l?p m?t cách thích h?p và nên biên d?ch,
Tuy nhiên, b?n có không ðý?c h? tr? XLL nêu ra. Cung c?p cho các bý?c c?n l?i
m?t s? m?u m?, b?n có th? thêm vào XLL ð? giúp b?n b?t ð?u.
N?i thêm 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;
}
//=================================================================
Thêm d?ng sau vào cu?i t?p tin Anewxll.def c?a b?n:
B?n bây gi? có th? biên 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 biên so?n, ð?i tên nó ð? Anewxll.xll.
B?ng cách s? d?ng ti?n ích trong v?i Microsoft Excel
B?t ð?u m?t b?ng tính m?i trong Microsoft Excel.
Nh?p vào Add-ins trên tr?nh ðõn công c?. Duy?t thêm 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 nãng ch?y.
Nh?p vào Add-ins trên tr?nh ðõn công c?. B? ch?n thêm 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 nãng ch?y.
Thêm ghi chú
N?u b?n ðang s? d?ng Microsoft Visual C++ 6.0, ví d? trý?c không thành công
khi b?n biên 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 xây d?ng, và bí quy?t XLL các nhà phát tri?n s? d?ng ð? ti?n t? chi?u dài dây c?a h? gây ra m?t s? vi ph?m truy c?p. Nhý m?t công vi?c-xung quanh, b?n có th? h?y b? /ZI tr?nh biên d?ch chuy?n trong thi?t l?p d? án c?a b?n, ho?c ch? c?n ki?m tra phiên b?n xây d?ng. Ð? bi?t thêm chi ti?t, xem bài vi?t sau trong cõ s? ki?n th?c Microsoft:
ID c?a bài: 178474 - 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 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? khóa:
kbhowto kbmt KB178474 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:178474