|
楼主 |
发表于 2020-3-17 11:45:01
|
显示全部楼层
谢谢两位大虾,问题解决了,呵呵。我把我的代码也帖上以后供大家交流。献丑:
#include <vcl.h>
#pragma hdrstop
#include "Comobj.hpp"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant ExcelApp,WorkBook1,Sheet1,Range;
bool bExcelRun = true;
HWND h = NULL;
h = FindWindow(0,"Microsoft Excel - 数据模板1");
if(h == NULL)
{
try
{
GetActiveOleObject("Excel.Application");
}
catch(...)
{
bExcelRun = false;
}
if(bExcelRun == false)
{
ExcelApp = Variant::CreateObject("Excel.Application");
}
else
{
ExcelApp = Variant::GetActiveObject("Excel.Application");
}
ExcelApp.PS("Visible",(Variant)true);
ExcelApp.PG("WorkBooks").FN("Add","F:\\mydata\\数据模板.xlt");
}
else
{
ExcelApp = Variant::GetActiveObject("Excel.Application");
WorkBook1 = ExcelApp.PG("ActiveWorkBook");
}
WorkBook1 = ExcelApp.PG("ActiveWorkBook");
Sheet1 = WorkBook1.PG("ActiveSheet");
Sheet1.PG("Cells",10,10).PS("Value",100.05);
}
呵呵。 |
|