📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <comobj.hpp>
#include <utilcls.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringGrid *sgrid;
int ii,jj,pp;
int CC;
AnsiString ass,assn;
Variant my_excel;
Variant all_workbooks;
Variant my_workbook;
Variant my_worksheet;
Variant my_range;
//
PropertyGet Range("Range");
PropertySet SetValue("Value");
//
sgrid=StringGrid1;
//
try
{
my_excel=GetActiveOleObject("excel.application");
}
catch(...)
{
try
{
my_excel=CreateOleObject("excel.application");
}
catch(...)
{
Application->MessageBox("不能连接Excel!","提示",MB_OK);
return;
}
}
my_excel.OlePropertySet("Visible",true);
all_workbooks=my_excel.OlePropertyGet("Workbooks");
// my_excel.OlePropertySet("SheetsInNewWorkbook",1);
my_workbook=all_workbooks.OleFunction("Add");
my_worksheet=my_excel.OlePropertyGet("ActiveSheet");
//
my_range=my_worksheet.Exec(Range<<"A1");
my_range.Exec(SetValue<<"输出自己的表");
Range.ClearArgs();
SetValue.ClearArgs();
//表头
CC='A';
//表内容
pp=2;
for(jj=0;jj<sgrid->RowCount;jj++)
{
for(ii=0;ii<sgrid->ColCount;ii++)
{
assn=char(CC+ii);
assn=assn+IntToStr(pp);
ass="";
ass=sgrid->Cells[ii][jj];
my_range=my_worksheet.Exec(Range<<assn);
my_range.Exec(SetValue<<ass);
Range.ClearArgs();
SetValue.ClearArgs();
}
pp++;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -