您可以使用 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++) 。