源代码如下,程序功能非常简单:打开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)