求大侠们帮忙,此程序运行出错,搞不懂咋回事啊
求大侠们帮忙,此程序运行出错,搞不懂怎么回事啊?
#include <vcl.h>
#pragma hdrstop
#include "U1_01.h"
#include <OleServer.hpp>
#include "Comobj.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TF1_1 *F1_1;
//---------------------------------------------------------------------------
__fastcall TF1_1::TF1_1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TF1_1::btnRunClick(TObject *Sender)
{ Variant ex,wb,sheet;
String abc;
try{
ex=Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0,"启动 Excel 出错","",MB_OK|MB_ICONERROR);
return;
}
ex.OlePropertyGet("WorkBooks").OleFunction("Open","C:\\shuju.xls");
ex.OlePropertySet("Visible", false);
wb=ex.OlePropertyGet("ActiveWorkBook");
wb.OlePropertyGet("Sheets",1).OleProcedure("Select");
sheet=wb.OlePropertyGet("ActiveSheet");
abc=sheet.OlePropertyGet("Cells",3,2).OlePropertyGet("Value");
Canvas->TextOutA(140,140,"abc");
ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Save");
ex.OlePropertyGet("Workbooks").OleFunction("close");
ex.OleFunction("Quit");
ShowMessage("设置成功!");
}
//---------------------------------------------------------------------------
void __fastcall TF1_1::btnEndClick(TObject *Sender)
{
Close();
}
运行结果:

------解决方案--------------------
ex.OlePropertyGet("WorkBooks").OleFunction("Open","C:\\shuju.xls");
改成:
ex.OlePropertyGet("WorkBooks").OleFunction("Open", WideString("C:\\shuju.xls"));
即可。
如果想用变量来代替,这样做:
一般人我不告诉他。
#include <vcl.h>
#pragma hdrstop
#include "U1_01.h"
#include <OleServer.hpp>
#include "Comobj.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TF1_1 *F1_1;
//---------------------------------------------------------------------------
__fastcall TF1_1::TF1_1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TF1_1::btnRunClick(TObject *Sender)
{ Variant ex,wb,sheet;
String abc;
try{
ex=Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0,"启动 Excel 出错","",MB_OK|MB_ICONERROR);
return;
}
ex.OlePropertyGet("WorkBooks").OleFunction("Open","C:\\shuju.xls");
ex.OlePropertySet("Visible", false);
wb=ex.OlePropertyGet("ActiveWorkBook");
wb.OlePropertyGet("Sheets",1).OleProcedure("Select");
sheet=wb.OlePropertyGet("ActiveSheet");
abc=sheet.OlePropertyGet("Cells",3,2).OlePropertyGet("Value");
Canvas->TextOutA(140,140,"abc");
ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Save");
ex.OlePropertyGet("Workbooks").OleFunction("close");
ex.OleFunction("Quit");
ShowMessage("设置成功!");
}
//---------------------------------------------------------------------------
void __fastcall TF1_1::btnEndClick(TObject *Sender)
{
Close();
}
运行结果:
------解决方案--------------------
ex.OlePropertyGet("WorkBooks").OleFunction("Open","C:\\shuju.xls");
改成:
ex.OlePropertyGet("WorkBooks").OleFunction("Open", WideString("C:\\shuju.xls"));
即可。
如果想用变量来代替,这样做:
String strXlsFile = "C:\\shuju.xls";
ex.OlePropertyGet("WorkBooks").OleFunction("Open", WideString(strXlsFile));
一般人我不告诉他。