15,981
社区成员




//从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));
// 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>
// 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;
}
#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" \
// 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);
}
}
// 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();
}
}