MFC如何向excel中插入图片?

xujianglun 2017-12-03 10:44:07

想向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));

...全文
1002 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
xujianglun 2017-12-15
  • 打赏
  • 举报
回复
引用 21 楼 zgl7903 的回复:
那就明确 引用 Font Gdiplus::Font font((HDC)0); FontPtr pFont
多谢, 我在代码中还没有使用这个,只是想先包含这个#import 然后想实现一个插入图片到excel的功能,
zgl7903 2017-12-15
  • 打赏
  • 举报
回复
那就明确 引用 Font Gdiplus::Font font((HDC)0); FontPtr pFont
xujianglun 2017-12-14
  • 打赏
  • 举报
回复
引用 19 楼 zgl7903 的回复:
这不是EXCEL的问题 http://blog.csdn.net/kisswawawa/article/details/8094824
引用 19 楼 zgl7903 的回复:
这不是EXCEL的问题 http://blog.csdn.net/kisswawawa/article/details/8094824
多谢, 这种方法我也试过, 貌是不行, 之前我也是按上面的方法试过 我在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>
zgl7903 2017-12-14
  • 打赏
  • 举报
回复
这不是EXCEL的问题 http://blog.csdn.net/kisswawawa/article/details/8094824
zgl7903 2017-12-13
  • 打赏
  • 举报
回复
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")
xujianglun 2017-12-13
  • 打赏
  • 举报
回复
引用 17 楼 zgl7903 的回复:
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")
多谢, 不过我按你上面的操作还是出现: ): 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” 冒是有两个地方有,
夫人的泡泡鱼 2017-12-13
  • 打赏
  • 举报
回复
zgl7903 2017-12-12
  • 打赏
  • 举报
回复
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;
}


xujianglun 2017-12-12
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
#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" \
多谢, 我在C:\Program Files\Microsoft Office\Office12 目录下找到对应的文件 但貌是还有问题,见上
xujianglun 2017-12-12
  • 打赏
  • 举报
回复
引用 12 楼 zgl7903 的回复:
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;
}


多谢,在 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”: 不明确的符号
赵4老师 2017-12-12
  • 打赏
  • 举报
回复
引用 10 楼 xujianglun 的回复:
C盘 D盘 Program Files(x86 )Program Files下面都找了 还是没有
安装Office 2003
赵4老师 2017-12-12
  • 打赏
  • 举报
回复
#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" \
赵4老师 2017-12-11
  • 打赏
  • 举报
回复
C:\\Program Files(x86)\\Common Files\\Microsoft Shared\\Office11\\mso.dll ?
xujianglun 2017-12-11
  • 打赏
  • 举报
回复
C盘 D盘 Program Files(x86 )Program Files下面都找了 还是没有
xujianglun 2017-12-11
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
C:\\Program Files(x86)\\Common Files\\Microsoft Shared\\Office11\\mso.dll ?
谢谢赵老师,我还是没有找到地方 不知道是不是要安装什么才有?
xujianglun 2017-12-10
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
在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();
    }
}


多谢 赵老师 不过我这边还是运行不了, C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll 这个目录 我电脑上没有这个文件,不知道是与装的excel想关吗? 这个是如何去看,改成与本机相关的?
xujianglun 2017-12-10
  • 打赏
  • 举报
回复
引用 4 楼 zgl7903 的回复:

// 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);
  }
}

多谢,我在加的时候还是会 fatal error C1083: 无法打开类型库文件:“MSO9.DLL”: No such file or directory 不知道 是不是还要在什么地方做下设置,
赵4老师 2017-12-07
  • 打赏
  • 举报
回复
赞4楼!
zgl7903 2017-12-06
  • 打赏
  • 举报
回复

// 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);
  }
}

赵4老师 2017-12-05
  • 打赏
  • 举报
回复
在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();
    }
}


加载更多回复(2)

15,979

社区成员

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

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