源代碼如下,程式功能非常簡單:打開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)
uj5u.com熱心網友回復:
求大佬們指點一下!不勝感激轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/54967.html
標籤:基礎類
