#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CreateExcel(ExcelPath)
{
Variant vExcelApp;
Variant WorkBook1;
Variant Sheet1;
Variant Range;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
"提示", MB_OK | MB_ICONERROR);
vExcelApp = Unassigned;
return;
}
// 隐藏Excel界面
vExcelApp.OlePropertySet("Visible", false);
// 新建一个工作表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
// 将剪贴板中的图像粘贴到Excel中
// vExcelApp.OlePropertyGet("ActiveSheet").OleProcedure("Paste");
Sheet1 = vExcelApp.PG("ActiveSheet");
Sheet1.PG("Cells",1,1).PS("Value","表格");
// Sheet1.PG("Cells",1,1).PG("Font").PS("Name","宋体");
// Sheet1.PG("Cells",1,1).PG("Font").PS("size","12");
// Sheet1.PG("Cells",1,1).PG("Font").PS("Bold",true); //加粗
Sheet1.PG("Cells",1,1).PS("NumberFormatLocal","000000"); //设置单元格格式为特殊
vExcelApp.PG("Cells",1,1).PS("ColumnWidth",22); //设置行宽
// vExcelApp.PG("Cells",1,1).PS("HorizontalAlignment",-4108); //设置居中
// 保存这个Excel文件
vExcelApp.OlePropertyGet("ActiveWorkbook")
.OleFunction("SaveAs", ExcelPath.c_str());
// 退出Excel
vExcelApp.OleFunction("Quit");
vExcelApp = Unassigned;
}
//---------------------------------------------------------------------------
c++ builder OLE创建Excel表格
最新推荐文章于 2021-11-30 14:32:50 发布