调用Excel出错,求帮助!!!

yuguiyang09 2013-09-04 10:57:00

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Comobj.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{

String a="C:\\table.xlt";
Variant Ex = CreateOleObject("Excel.Application");
Ex.OlePropertyGet("workbooks").OleFunction("Add",a.w_str() );
Variant Wb = Ex.OlePropertyGet("ActiveWorkBook");
Variant Sheet = Wb.OlePropertyGet("ActiveSheet");
Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("Value", 1);
Ex.OlePropertySet("Visible", true);
}
//---------------------------------------------------------------------------

执行后报错,但将c盘下的table.xlt模板文件重命名为两位的ta.xlt,a="C:\\ta.xlt"就可以调用模板,好像是字符串转换的问题,如果换比较长的路径该怎么改,请高手指教。
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuguiyang09 2013-09-04
  • 打赏
  • 举报
回复
嗯,可以了,谢谢妖哥出马,新手学习中,让妖哥见笑了,分数不多请笑纳,呵呵。
yuguiyang09 2013-09-04
  • 打赏
  • 举报
回复
忘了说明一下,是在XE3下编译的出错。
ccrun.com 2013-09-04
  • 打赏
  • 举报
回复
OLE专业户来也!!!
String a = "C:\\table.xlt"; // 随便换文件名
Variant Ex = CreateOleObject("Excel.Application");
Ex.OlePropertyGet("workbooks").OleFunction("Add", WideString(a));

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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