想向excel中插入图片,找了如下方法,发现没有Shapes
//从Sheet对象上获得一个Shapes    
Shapes shapes=sheet.GetShapes();
//获得Range对象,用来插入图片
range=sheet.GetRange(COleVariant("B16"),COleVariant("J22"));
rgMyRge1=range;
//从本地添加一个图片
shapes.AddPicture("D:\\Test1.jpg",false,true,(float)range.GetLeft().dblVal,(float)range.GetTop().dblVal,(float)range.GetWidth().dblVal,(float)range.GetHeight().dblVal);
//设置宽高
ShapeRange sRange=shapes.GetRange(_variant_t(long(1)));
sRange.SetHeight(float(30));
sRange.SetWidth(float(30));

解决方案 »

  1.   

    http://bbs.csdn.net/topics/360078588
      

  2.   


    谢谢,
    但在那里我还没有找到可用的,
    void CtestxlsDlg::OnBnClickedButton1()
    {
        // TODO: 在此添加控件通知处理程序代码
        //获取当前应用程序目录
        TCHAR path[MAX_PATH];
        CString strPath;
        GetModuleFileName(NULL,path,MAX_PATH);
        strPath = path;
        int nPos = strPath.ReverseFind('\\');
        strPath=strPath.Left(nPos+1);
        strPath += L"test.xls";    HANDLE hFind; 
        WIN32_FIND_DATA findData = {0};
        hFind=FindFirstFile(strPath,&findData);
        if(hFind == INVALID_HANDLE_VALUE) 
        {
            CString TempfileStr;
            TempfileStr.Format(L"XLS文件'%s'不存在",strPath);
            AfxMessageBox(TempfileStr);
            return;
        }
        //////////////////////////////////////////////////////////////////////////
        Excel::_ApplicationPtr pApplication = NULL;
        Excel::_WorkbookPtr pThisWorkbook = NULL;
        Excel::_WorksheetPtr pThisWorksheet = NULL;
        Excel::SheetsPtr pThisSheets = NULL;
        Excel::RangePtr pThisRange = NULL;
        Excel::ShapesPtr shapes=NULL;
        Excel::ShapePtr shape=NULL;
        Excel::LinesPtr lines=NULL;    
        pApplication.CreateInstance("Excel.Application");
        pApplication->PutVisible(0,VARIANT_FALSE);                    //将Excel程序隐藏
        pApplication->PutDisplayAlerts(0,VARIANT_FALSE);            //不弹出消息框(如果有)
        //////////////////////////////////////////////////////////////////////////
        pThisWorkbook = pApplication->GetWorkbooks()->Add(_variant_t(strPath));        //读模版
        pThisSheets = pThisWorkbook->GetWorksheets();
        pThisWorksheet = pThisSheets->GetItem((short)1);
        pThisRange = pThisWorksheet->GetRange("A1");
        shapes = pThisWorksheet->GetShapes();
        lines = pThisWorksheet->Lines();
        //////////////////////////////////////////////////////////////////////////
        CString Name1,Name2,Name3,tmpStr;
        //////////////////////////////////////////////////////////////////////////
        shapes->AddShape(msoShapeRectangle,200,100,20,20);    //此图形单独,不需要组合
        //以下图形为一个整体,需要组合
        shape=shapes->AddShape(msoShapeOval,100,100,20,20);    //圆
        Name1=shape->GetName().GetBSTR();
        shape=shapes->AddLine(100,100,120,120);    //画线
        Name2=shape->GetName().GetBSTR();
        shape=shapes->AddLine(120,100,100,120);    //画线
        Name3=shape->GetName().GetBSTR();
        //////////////////////////////////////////////////////////////////////////
        //选中上面添加的后三个图形,并实现组合功能
        SAFEARRAY* pSafeArray = SafeArrayCreateVectorEx(VT_VARIANT, 1, 3, NULL);
        if (pSafeArray)
        {
            VARIANT* apVariantData;
            HRESULT hResult = SafeArrayAccessData(pSafeArray, reinterpret_cast<void**> (&apVariantData));
            if (SUCCEEDED(hResult))
            {
                apVariantData[0].vt = VT_BSTR;
                apVariantData[0].bstrVal = SysAllocString(Name1);
                apVariantData[1].vt = VT_BSTR;
                apVariantData[1].bstrVal = SysAllocString(Name2);
                apVariantData[2].vt = VT_BSTR;
                apVariantData[2].bstrVal = SysAllocString(Name3);
                SafeArrayUnaccessData(pSafeArray);
            }
            CComVariant rangeIndexes(pSafeArray);
            pThisWorksheet->GetShapes()->GetRange(&rangeIndexes)->Group();
            SafeArrayDestroy(pSafeArray);
        }    //excel中的vba实现代码如下:
        //ActiveSheet.Shapes.Range(Array("Oval 2", "Line 3", "Line 4")).Select
        //Selection.ShapeRange.Group.Select
        
        //////////////////////////////////////////////////////////////////////////
        pApplication->PutDisplayAlerts(0,VARIANT_TRUE);            //弹出消息框(如果有)
        pApplication->PutVisible(0,VARIANT_TRUE);
    }看到提到用上面的方式,但不知需什么头文件?  Excel::
      

  3.   

    在Excel中开始记录宏,手动完成所需功能,结束记录宏,按Alt+F11键,查看刚才记录的宏对应的VBA代码。
    仅供参考:// Copyright (C) 1992-1998 Microsoft Corporation
    // All rights reserved.
    //
    // This source code is only intended as a supplement to the
    // Microsoft Visual C++ Language  Reference and related
    // electronic documentation provided with Microsoft Visual C++.
    // See these sources for detailed information regarding the
    // Microsoft Visual C++ product.// NOTE: This example will only work with Excel8 in Office97
    // Compile with cl /GX comexcel.cpp
    // TO DO: Edit the #import paths
    //#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb.  Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
    #import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
    #import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
    #import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces#pragma warning (disable:4192 4146)#include <stdio.h>
    #include <tchar.h>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);
    }// If this is placed in the scope of the smart pointers, they must be
    // explicitly Release(d) before CoUninitialize() is called.  If any reference
    // count is non-zero, a protection fault will occur.
    struct StartOle
    {
        StartOle() { CoInitialize(NULL); }
        ~StartOle() { CoUninitialize(); }
    } _inst_StartOle;void main()
    {
        using namespace Excel;    _ApplicationPtr pXL;    try
        {
          pXL.CreateInstance(L"Excel.Application");      pXL->Visible = VARIANT_TRUE;      WorkbooksPtr pBooks = pXL->Workbooks;
          _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
          _WorksheetPtr pSheet = pXL->ActiveSheet;      RangePtr pRange;
          pRange = pSheet->Range["A21"];
          pRange->Value2 = 75L;
          //pRange->NumberFormatLocal = "@";      _CommandBarsPtr pCmdbars = pXL->CommandBars;
          int iCmdbars = pCmdbars->GetCount();      Sleep(1000);
          pRange = pSheet->Range["20:20"];
          pRange->Insert( (long)Excel::xlDown );
          pRange->Merge();      Sleep(1000);      pBook->Saved = VARIANT_TRUE;
          pXL->Quit();
        }
        catch(_com_error &e)
        {
          dump_com_error(e);      pXL->Quit();
        }
    }
      

  4.   


    // Copyright (C) 1992-1998 Microsoft Corporation
    // All rights reserved.
    //
    // This source code is only intended as a supplement to the
    // Microsoft Visual C++ Language  Reference and related
    // electronic documentation provided with Microsoft Visual C++.
    // See these sources for detailed information regarding the
    // Microsoft Visual C++ product.// NOTE: This example will only work with Excel8 in Office97
    // Compile with cl /GX comexcel.cpp
    // TO DO: Edit the #import paths
    #import "MSO9.DLL"  rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
      rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
    #import "VBE6EXT.OLB" no_namespace no_auto_exclude  
    #import "EXCEL9.OLB" rename("RGB", "RBGXL") rename("DialogBox", "DialogBoxXL") \
      rename("CopyFile", "CopyFileXL") rename("ReplaceText", "ReplaceTextXL") rename("IPicture", "IPictureXL") \
      rename("IFont", "IFontXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces no_auto_exclude #include <stdio.h>
    #include <tchar.h>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);
    }// If this is placed in the scope of the smart pointers, they must be
    // explicitly Release(d) before CoUninitialize() is called.  If any reference
    // count is non-zero, a protection fault will occur.
    struct StartOle {
        StartOle() { CoInitialize(NULL); }
        ~StartOle() { CoUninitialize(); }
    } _inst_StartOle;
    void main()
    {
      using namespace Office;
      using namespace Excel;
      
      _ApplicationPtr pXL;
      
      try 
      {
        pXL.CreateInstance(L"Excel.Application");
        
        pXL->Visible = 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( "C:\\tt.jpg", 
          msoFalse, msoTrue, l, t, w, h);
        
        
        pSheet->Range["A2"]->Value = "Company A";
        pSheet->Range["B2"]->Value = "Company B";
        pSheet->Range["C2"]->Value = "Company C";
        pSheet->Range["D2"]->Value = "Company D";
        
        pSheet->Range["A3"]->Value = 75.0;
        pSheet->Range["B3"]->Value = 14.0;
        pSheet->Range["C3"]->Value = 7.0;
        pSheet->Range["D3"]->Value = 4.0;
        
        Sleep(1000);
        
        RangePtr  pRange  = pSheet->Range["A2:D3"];
         _ChartPtr  pChart  = pBook->Charts->Add();
        
        pChart->ChartWizard((Range*) pRange, (long) xl3DPie, 7L, (long) xlRows,
          1L, 0L, 2L, "Market Share");
        
        Sleep(6000);
        
        pBook->Saved = VARIANT_TRUE;
        pXL->Quit();
      } 
      catch(_com_error &e) 
      {
        dump_com_error(e);
      }
    }
      

  5.   


    多谢,我在加的时候还是会
     fatal error C1083: 无法打开类型库文件:“MSO9.DLL”: No such file or directory
    不知道 是不是还要在什么地方做下设置,
      

  6.   


    多谢 赵老师   不过我这边还是运行不了,
    C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll
    这个目录  我电脑上没有这个文件,不知道是与装的excel想关吗?
    这个是如何去看,改成与本机相关的?
      

  7.   

    C:\\Program Files(x86)\\Common Files\\Microsoft Shared\\Office11\\mso.dll ?
      

  8.   

     C盘 D盘  Program Files(x86 )Program Files下面都找了  还是没有
      

  9.   

    安装Office 2003
      

  10.   

    Office2007 也差不多// 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")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( "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);
        
        pBook->PutSaved(0, VARIANT_TRUE);
        pXL->Quit();
      } 
      catch(_com_error &e) 
      {
        dump_com_error(e);
      }  return 0;
    }
      

  11.   

    #import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\mso.dll" \
    #import "C:\\Program Files\\Common Files\\Microsoft Shared\\vba\\VBA6\\VBE6EXT.OLB" no_namespace
    #import "C:\\Program Files\\Microsoft Office\\OFFICE12\\excel.exe" \
      

  12.   

    多谢,在 C:/Program Files/Microsoft Office/OFFICE12/mso.dll" 目录找到了对应文件,但
    还是有下面错误,
     error C2872: “Font”: 不明确的符号
    1>          可能是“D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\comdef.h(312) : Font”
    1>          或       “c:\program files (x86)\microsoft sdks\windows\v7.0a\include\GdiplusHeaders.h(244) : Gdiplus::Font”
    1>TestResultTab.cpp(39): error C2872: “Font”: 不明确的符号
      

  13.   

    多谢,
    我在C:\Program Files\Microsoft Office\Office12  目录下找到对应的文件
    但貌是还有问题,见上
      

  14.   

    再参考下这个链接http://blog.csdn.net/ouyangyanlan/article/details/48597951
      

  15.   

    Import 时 重命名Font空间
    #import "C:/Program Files/Microsoft Office/OFFICE12/excel.exe" \
      rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
      rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \
      rename("IFont", "IFontXL")
      

  16.   


    多谢,
    不过我按你上面的操作还是出现:
    ): error C2872: “Font”: 不明确的符号
    1>          可能是“D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\comdef.h(312) : Font”
    1>          或       “c:\program files (x86)\microsoft sdks\windows\v7.0a\include\GdiplusHeaders.h(244) : Gdiplus::Font”冒是有两个地方有,
      

  17.   

    这不是EXCEL的问题 http://blog.csdn.net/kisswawawa/article/details/8094824
      

  18.   

    多谢,
    这种方法我也试过,
    貌是不行,
    之前我也是按上面的方法试过
    我在stdafx.h中加入
    // stdafx.h : 标准系统包含文件的包含文件,
    // 或是经常使用但不常更改的
    // 特定于项目的包含文件#pragma once#define _ATL_APARTMENT_THREADED 
    #ifndef _SECURE_ATL
    #define _SECURE_ATL 1
    #endif#ifndef VC_EXTRALEAN
    #define VC_EXTRALEAN            // 从 Windows 头中排除极少使用的资料
    #endif#include "targetver.h"
    #include <io.h>
    #include <fcntl.h>
    #include <stdio.h>
    #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // 某些 CString 构造函数将是显式的// 关闭 MFC 对某些常见但经常可放心忽略的警告消息的隐藏
    #define _AFX_ALL_WARNINGS#include <afxwin.h>         // MFC 核心组件和标准组件
    #include <afxext.h>         // MFC 扩展
    #include <afxdisp.h>        // MFC 自动化类#ifndef _AFX_NO_OLE_SUPPORT
    #include <afxdtctl.h>           // MFC 对 Internet Explorer 4 公共控件的支持
    #endif
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h>             // MFC 对 Windows 公共控件的支持
    #endif // _AFX_NO_AFXCMN_SUPPORT#include <afxcontrolbars.h>     // 功能区和控件条的 MFC 支持#include <GdiPlus.h>
    #pragma comment(lib, "gdiplus.lib")
    using namespace Gdiplus;
    #ifdef _UNICODE
    #if defined _M_IX86
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #elif defined _M_X64
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #else
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #endif
    #endif#include <atlbase.h>
    #include <atlcom.h>
    #include <atlctl.h>
      

  19.   

    那就明确 引用 Font  Gdiplus::Font font((HDC)0);
    FontPtr pFont
      

  20.   

    多谢,
    我在代码中还没有使用这个,只是想先包含这个#import 然后想实现一个插入图片到excel的功能,