C++ builder 操作EXECL文件疑问

lykulou 2017-10-10 12:16:25
源代码如下,程序功能非常简单:打开C:\1.xlsx,将其另存为C:\2.xlsx
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
#include "Comobj.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant Ex, Wb, St;
try
{
Ex = Variant::CreateObject ((WideString)(L"Excel.Application"));
}
catch(...)
{
ShowMessage("运行Excel出错,请确认安装了Office");
return;
}

Ex.PS(WideString(L"Visible"),false);
Ex.PG(WideString(L"WorkBooks")).PR(WideString(L"Open"),WideString(L"c:\\1.xlsx"));
Wb = Ex.PG(WideString(L"ActiveWorkBook"));
St = Wb.PG(WideString(L"ActiveSheet"));
Wb.PR(WideString(L"SaveAs"),WideString(L"c:\\2.xlsx")); //保存
Wb.PR(WideString(L"Close")); //关闭
Ex.FN(WideString(L"Quit")); //退出

Ex = Unassigned;
Wb = Unassigned;
St = Unassigned;
MessageBox(NULL,"运行Excel完毕 !!!","提示",MB_OK);
}
该程序执行时遇到问题: 只要这里设置为false Ex.PS(WideString(L"Visible"),false);
程序最后必定会报错

但是只要把Ex.PS(WideString(L"Visible"),false); 这里的false 改成true 就不会出项上面的错误!
请各位帮忙指点一下,本人新手中的新手!谢谢!
(WIN7 64位+EXECL2007+BCB2010)
...全文
197 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lykulou 2017-10-16
  • 打赏
  • 举报
回复
求大佬们指点一下!不胜感激

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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