通過MFC寫Excel,并給特定的單元格設超鏈接
//建立超鏈接
CString cc; //超鏈接¨地址
cc =_T("http://www.baidu.com/index.php?tn=maxthon2&ch=3");//
range.AttachDispatch(sheet.GetRange(_variant_t(_T("C2")),_variant_t(_T("C2")))); //獲得區域
links.AttachDispatch(range.GetHyperlinks()); //獲得Hyperlinks物件
此句報錯 直接中斷 找不到有用的錯誤資訊
links.Add(range,cc,_variant_t(_T("")),_variant_t(_T("")),_variant_t(_T(""))); //設定超鏈接
請大神解惑
uj5u.com熱心網友回復:
@weixin_38323761uj5u.com熱心網友回復:
EXCEL中錄制宏, 手動操作, 按宏代碼編輯程式
// 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;
pSheet->Name = "Market Share!";
//插入超鏈接
try
{
//Range("I4").Select
//ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
// "http://bbs.csdn.net/topics/392326155", TextToDisplay:="測驗超鏈接"
RangePtr pRang = pSheet->Range["I4"];
HyperlinksPtr pLinks = pSheet->Hyperlinks;
pLinks->Add(pRang,
"http://bbs.csdn.net/topics/392326155",
vtMissing,
vtMissing,
"測驗超鏈接");
}
catch(_com_error &e)
{
dump_com_error(e);
}
pSheet->Range["A2"]->Value2 = "Company A";
pSheet->Range["A3"]->Value2 = 75.0;
pSheet->Range["A4"]->Value2 = 75.0;
Sleep(6000);
pBook->PutSaved(0, VARIANT_TRUE);
pXL->Quit();
}
catch(_com_error &e)
{
dump_com_error(e);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/73811.html
上一篇:大家幫忙看看
