求助OLE大神,excel如何插入超链接

123456jiihi 2015-04-29 10:51:45
如题,我想在导出EXCEL时把图片和视频先复制到存放EXCEL的文件夹内,然后同意操作OLE设置EXCEL超链接链接到EXCEL当前文件夹的图片或视频上,当点击EXCEL中图片名字超链接时打开图片,这个该怎么做 求大神们帮助啊 用的是CB XE7 和EXCEL 2007.
...全文
764 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccrun.com 2015-04-29
  • 打赏
  • 举报
回复
Variant vExcelApp;
try
{
	vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
	MessageBoxW(0, L"启动 Excel 出错, 可能是没有安装Excel.",
			L"Error", MB_OK | MB_ICONERROR);
	vExcelApp = Unassigned;
	return;
}

vExcelApp.OlePropertySet("Visible", true);

String strXlsFile = L"D:\\ccrun\\123.xls";
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", WideString(strXlsFile));

Variant vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);

// 在第二行第三列的格子里插入超链接
vSheet.OlePropertyGet("Cells", 2, 3).OleProcedure("Select");
String strLinkFile = L"D:\\ccrun\\123.bmp";
String strScreenTip = L"点击打开图片";
String strTextToDisplay = L"链接图片";
vSheet.OlePropertyGet("Hyperlinks").OleFunction("Add",
		vExcelApp.OlePropertyGet("Selection"), // Anchor
		WideString(strLinkFile),               // Address
		WideString(strLinkFile),               // SubAddress
		WideString(strScreenTip),              // ScreenTip
		WideString(strTextToDisplay)           // TextToDisplay
		);
...
123456jiihi 2015-04-29
  • 打赏
  • 举报
回复
不愧是妖哥,果然神人也 附上我完成的函数

void __fastcall TfmMain::ADOQueryExcel(TADOQuery *dbg, String strXlsFile)
{
	///修改妖哥(ccrun)的 DBGRID转EXCEL,改为ADOQUERY 转EXCEL
	if(!dbg->Active) // 数据集没有打开就返回
	{	return;}
	Variant vExcelApp, vSheet;
	try
	{
		vExcelApp = Variant::CreateObject("Excel.Application");
	}
	catch(...)
	{
		MessageBox(0, L"启动 Excel 出错, 可能是没有安装Excel.",
				L"DBGrid2Excel", MB_OK);
		return;
	}
	// 隐藏Excel界面
	vExcelApp.OlePropertySet((WideString)L"Visible", false);
	// 新建一个工作表
	vExcelApp.OlePropertyGet((WideString)L"Workbooks").OleFunction((WideString)L"Add"); // 工作表
	// 操作这个工作表
	vSheet = vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
			.OlePropertyGet((WideString)L"Sheets", 1);
	//设置Excel文档的字体
	vSheet.OleProcedure((WideString)L"Select");
	vSheet.OlePropertyGet((WideString)L"Cells").OleProcedure((WideString)L"Select");
	vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
			.OlePropertySet((WideString)L"Size", 12);
	vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
			.OlePropertySet((WideString)L"Name", L"新宋体");
	vExcelApp.OlePropertyGet((WideString)L"Selection").OlePropertyGet((WideString)L"Font")
			.OlePropertySet((WideString)L"FontStyle", L"常规");
	vSheet.OlePropertyGet((WideString)L"Cells", 1, 1).OleProcedure((WideString)L"Select");
    // 表格的行数
	int nRowCount=dbg->RecordCount+1;	// 表格的列数
	int nColCount=dbg->FieldCount; //表格的行数
	nColCount = nColCount < 1? 1: nColCount;
	//设置单元格的宽度
	 for(int i=0; i<nColCount; i++)
	{
		//int nColWidth = dbg->Columns->Items[i]->Width;
		vSheet.OlePropertyGet((WideString)L"Columns",i+1).OlePropertySet((WideString)L"ColumnWidth",10);
	}
	// 先将列名写入Excel表格
	for(int j=0; j<nColCount; j++)
	{
		// 标题行的行高
		vExcelApp.OlePropertyGet((WideString)L"Rows", 1).OlePropertySet((WideString)L"RowHeight", 20);
		//WideString tem=
		vSheet.OlePropertyGet((WideString)L"Cells", 1, j + 1)
				.OlePropertySet((WideString)L"Value",
				(WideString)dbg->Fields->Fields[j]->FieldName);
		// 设置列名单元格的背景色
		Variant vInter = vSheet.OlePropertyGet(
				(WideString)L"Cells", 1, j + 1).OlePropertyGet((WideString)L"Interior");
		vInter.OlePropertySet((WideString)L"ColorIndex", 15); // 灰色
		vInter.OlePropertySet((WideString)L"Pattern", 1); // xlSolid
		vInter.OlePropertySet((WideString)L"PatternColorIndex", -4105); // xlAutomatic
	}
	// 将ADOQuery中的数据写入Excel表格
	dbg->First();
	for(int i=0; i<nRowCount-1; i++)
	{
		// 普通数据行的行高16
		vExcelApp.OlePropertyGet((WideString)L"Rows", i + 2).OlePropertySet((WideString)L"RowHeight", 16);
		// 63 63 72 75 6E 2E 63 6F 6D
		for(int j=0; j<dbg->FieldCount; j++)
		{
				if((dbg->Fields->Fields[j]->FieldName=="图片1")||||(dbg->Fields->Fields[j]->FieldName=="图片2")||(dbg->Fields->Fields[j]->FieldName=="图片3"))
				{
				WideString data1=dbg->FieldByName(dbg->Fields->Fields[j]->FieldName)->AsString;
				WideString data2=ExtractFileName(data1);
				vSheet.OlePropertyGet((WideString)L"Cells", i + 2, j + 1).OleProcedure("Select");
				String strLinkFile = L".\\"+data2;
				String strScreenTip = L"点击打开图片";
				String strTextToDisplay = L"链接图片";
				vSheet.OlePropertyGet("Hyperlinks").OleFunction("Add",
				vExcelApp.OlePropertyGet("Selection"), // Anchor
				WideString(strLinkFile),               // Address
				WideString(strLinkFile),               // SubAddress
				WideString(strScreenTip),              // ScreenTip
				WideString(strTextToDisplay)           // TextToDisplay
				);
				}
				else
				{
				Variant vCell = vSheet.OlePropertyGet((WideString)L"Cells", i + 2, j + 1);
				WideString data="'"+dbg->FieldByName(dbg->Fields->Fields[j]->FieldName)->AsString;
				vCell.OlePropertySet((WideString)L"Value",data);
				}
		}
		dbg->Next();
	}
	dbg->First();
	// 保存Excel文档并退出
	WideString str=strXlsFile;
	vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
			.OleFunction((WideString)L"SaveAs",str);
	vExcelApp.OlePropertyGet((WideString)L"ActiveWorkbook")
			.OleFunction((WideString)L"Close");
	vExcelApp.OleFunction((WideString)L"Quit");
	vSheet = Unassigned;
	vExcelApp = Unassigned;
	// 工作结束
	MessageBox(0, L"生成excel表格成功!",
			L"消息", MB_OK);
}
//---------------------------------------------------------------------------

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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