您可以使用OleObjects將任何文件(任何類型)嵌入到Excel工作表中。這是安裝文件示例代碼:
BOOL InsertFileToWorksheet(Exl_Worksheet &ws, LPCTSTR lpFilePath, LPCTSTR ObjName)
{
RemoveOldOleObj(ws,ObjName);
VARIANT vFileName;
str_to_vt(vFileName,(char*)lpFilePath);
ExlOLEObjects ole_objects;
BOOL bRes(TRUE);
try{
ole_objects = ws.OLEObjects(vOptional);
COleVariant left(10l),top(10l),width(1l),height(1l);
Exl_OLEObject objSrc = ole_objects.Add(vOptional,vFileName,vOptional,vOptional,vOptional,vOptional,vOptional,
vOptional,vOptional,vOptional,vOptional);
objSrc.SetName(ObjName);
}catch(...){
bRes = FALSE;
}
VariantClear(&vFileName);
return bRes;
}
void RemoveOldOleObj(Exl_Worksheet &ws, LPCTSTR ObjName)
{
ExlOLEObjects ole_objects;
try{
ole_objects = ws.OLEObjects(vOptional);
}catch(...){
return;
}
VARIANT vObjName;
str_to_vt(vObjName,(char*)ObjName);
Exl_OLEObject obj;
try{
obj = ole_objects.Item(vObjName);
obj.Delete();
}catch(...){
}
VariantClear(&vObjName);
}
完整的源代碼(C++),你可以找到in this article。