想向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));
uj5u.com熱心網友回復:
http://bbs.csdn.net/topics/360078588uj5u.com熱心網友回復:
謝謝,
但在那里我還沒有找到可用的,
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::
uj5u.com熱心網友回復:
在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();
}
}
uj5u.com熱心網友回復:
// 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);
}
}
uj5u.com熱心網友回復:
贊4樓!
uj5u.com熱心網友回復:
多謝,我在加的時候還是會
fatal error C1083: 無法打開型別庫檔案:“MSO9.DLL”: No such file or directory
不知道 是不是還要在什么地方做下設定,
uj5u.com熱心網友回復:
多謝 趙老師 不過我這邊還是運行不了,
C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll
這個目錄 我電腦上沒有這個檔案,不知道是與裝的excel想關嗎?
這個是如何去看,改成與本機相關的?
uj5u.com熱心網友回復:
C:\\Program Files(x86)\\Common Files\\Microsoft Shared\\Office11\\mso.dll ?uj5u.com熱心網友回復:
謝謝趙老師,我還是沒有找到地方
不知道是不是要安裝什么才有?
uj5u.com熱心網友回復:
C盤 D盤 Program Files(x86 )Program Files下面都找了
還是沒有
uj5u.com熱心網友回復:
安裝Office 2003
uj5u.com熱心網友回復:
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;
}
uj5u.com熱心網友回復:
#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" \
uj5u.com熱心網友回復:
多謝,在 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”: 不明確的符號
uj5u.com熱心網友回復:
多謝,
我在C:\Program Files\Microsoft Office\Office12 目錄下找到對應的檔案
但貌是還有問題,見上
uj5u.com熱心網友回復:
再參考下這個鏈接http://blog.csdn.net/ouyangyanlan/article/details/48597951uj5u.com熱心網友回復:
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")
uj5u.com熱心網友回復:
多謝,
不過我按你上面的操作還是出現:
): 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”
冒是有兩個地方有,
uj5u.com熱心網友回復:
這不是EXCEL的問題http://blog.csdn.net/kisswawawa/article/details/8094824
uj5u.com熱心網友回復:
多謝,
這種方法我也試過,
貌是不行,
之前我也是按上面的方法試過
我在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>
uj5u.com熱心網友回復:
那就明確 參考 FontGdiplus::Font font((HDC)0);
FontPtr pFont
uj5u.com熱心網友回復:
多謝,
我在代碼中還沒有使用這個,只是想先包含這個#import
然后想實作一個插入圖片到excel的功能,
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/79680.html
標籤:界面
上一篇:為什么GetDC(hwnd),獲取在hdc,用BitBlt傳送后是空的?
下一篇:尋個4年前用mfc寫棋牌的一個牛
