mfc 向excel 中插入图片

xujianglun 2017-12-31 08:42:51
想实现向excel 中插入图片,一直没有实现,总 是有这那的问题
在网上找了一个例程

void CInsertImageDlg::OnOpenexcel()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"All Files(*.xls)|*.xls||",AfxGetMainWnd()); //构造文件打开对话框
CString strPath; //声明变量
if(dlg.DoModal() == IDOK) //判断是否按下"打开"按钮
{
strPath = dlg.GetPathName(); //获得文件路径
m_ePath.SetWindowText(strPath); //显示文件路径
}

}

void CInsertImageDlg::OnOpenimage()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"图片文件|*.bmp;*.jpg;*.jpeg||",AfxGetMainWnd()); //构造文件打开对话框
CString strPath; //声明变量
if(dlg.DoModal() == IDOK) //判断是否按下"打开"按钮
{
strPath = dlg.GetPathName(); //获得图片文件路径
m_iPath.SetWindowText(strPath); //显示文件路径
}

}

void CInsertImageDlg::OnButinsert()
{
// TODO: Add your control notification handler code here
CString ePath,iPath;
m_ePath.GetWindowText(ePath);
m_iPath.GetWindowText(iPath);

_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Shapes shp;

//创建Excel 2000服务器(启动Excel)
if (!app.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
exit(1);
}
books.AttachDispatch(app.GetWorkbooks());
book.AttachDispatch(books.Add(_variant_t(ePath)));
//得到Worksheets
sheets.AttachDispatch(book.GetWorksheets());
sheet.AttachDispatch(sheets.GetItem(_variant_t("Sheet1")));
shp.AttachDispatch(sheet.GetShapes());
shp.AddPicture(iPath,false,true,0,0,400,300);

app.SetVisible(true);
//释放对象
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
}

也是有错误,

在此请教下大家
...全文
544 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2018-01-02
  • 打赏
  • 举报
回复

// Excel12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#import "C:/Program Files/Microsoft Office/OFFICE12/mso.dll" \
  rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
  rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
#import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT.OLB" no_namespace
#import "C:/Program Files/Microsoft Office/OFFICE12/excel.exe" \
  rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
  rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \
  rename("IFont", "IFontXL")

void dump_com_error(_com_error &e)
{
  _tprintf(_T("Oops - hit an error!\n"));
  _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  _bstr_t bstrSource(e.Source());
  _bstr_t bstrDescription(e.Description());
  _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
struct StartOle {
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;


int main(int argc, char* argv[])
{ 
  using namespace Office;
  using namespace Excel;
  
  _ApplicationPtr pXL;
  
  try 
  {
    pXL.CreateInstance(L"Excel.Application");
    
    pXL->PutVisible(0, VARIANT_TRUE);
    
    WorkbooksPtr pBooks = pXL->Workbooks;
    _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
    
    _WorksheetPtr pSheet = pXL->ActiveSheet;
    
    try
    {
      // This one will fail
      pSheet->Name = "Market Share?";
    }
    catch (_com_error &e) 
    {
      dump_com_error(e);
    }
    
    pSheet->Name = "Market Share!";

    Excel::ShapesPtr pShapes = pSheet->Shapes;
    RangePtr range = pSheet->Range["H1:O10"];
    float l = range->Left, t = range->Top;
    float w = range->Width, h = range->Height;
    Excel::ShapePtr pShape = pShapes->AddPicture( _bstr_t("C:\\tt.jpg"), 
      msoFalse, msoTrue, l, t, w, h);
        
    pSheet->Range["A2"]->Value2 = "Company A";
    pSheet->Range["B2"]->Value2 = "Company B";
    pSheet->Range["C2"]->Value2 = "Company C";
    pSheet->Range["D2"]->Value2 = "Company D";
    
    pSheet->Range["A3"]->Value2 = 75.0;
    pSheet->Range["B3"]->Value2 = 14.0;
    pSheet->Range["C3"]->Value2 = 7.0;
    pSheet->Range["D3"]->Value2 = 4.0;

    pSheet->Range["A4"]->Value2 = 75.0;
    pSheet->Range["B4"]->Value2 = 14.0;
    pSheet->Range["C4"]->Value2 = 7.0;
    pSheet->Range["D4"]->Value2 = 4.0;
    
    Sleep(6000);

    PageSetupPtr pPageSet = pSheet->PageSetup;
    pPageSet->PutPaperSize( xlPaperA4 ); //A4
    pPageSet->CenterHorizontally = VARIANT_TRUE;  //水平居中
    pPageSet->PutOrientation( xlLandscape ); //横向打印
    pPageSet->PrintArea = "$A$1:$J$22"; //打印区域
    
    pBook->PutSaved(0, VARIANT_TRUE);
    pXL->Quit();
  } 
  catch(_com_error &e) 
  {
    dump_com_error(e);
  }

  return 0;
}



xujianglun 2018-01-01
  • 打赏
  • 举报
回复
像这种比较常见的功能,看别人评论是可以正常运行, 不知道是不是环境上要做什么设置吗?
xujianglun 2017-12-31
  • 打赏
  • 举报
回复
继续跟进 在 如图位置 后

InsertImage.exe 中的 0x7524c54f (KernelBase.dll) 处最可能的异常: Microsoft C++ 异常: 内存位置 0x0037e864 处的 COleDispatchException。
xujianglun 2017-12-31
  • 打赏
  • 举报
回复
跟踪代码
发现出现下面问题,
这里不是 很明白,谢谢哪位朋友帮忙解析下,

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧