📄 mainform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Mainform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Excel_2K_SRVR"
#include "Excel_2K_SRVR.h"
#include <OleServer.hpp>
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Form1OnCreate(TObject *Sender)
{
for(int j=1;j<10;j++)
{
for(int i =1;i<10; i++)
{
this->StringGrid1->Cells[j-1][i-1]=IntToStr(i*j);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
AnsiString StrFileName=GetCurrentDir()+"\\Export.xls";
if(!FileExists(StrFileName))
{
MessageBox(Handle,"Excel文件Export.xls不存在!","信息提示",MB_OK);
return;
}
Variant ExcelApp; //对象
Variant WorkBook1; //工作薄
Variant WorkSheet1; //工作表
Variant Range; //范围
Variant Borders; // 边框
try
{
ExcelApp=Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(Handle,"无法启动Excel!","信息提示",MB_ICONSTOP|MB_OK);
return;
}
ExcelApp.OlePropertySet("Visible",false);//设置Excel为不可见
//打开指定的Excel文件,文件中最好只设定一个Sheet
ExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open",StrFileName.c_str());
WorkBook1=ExcelApp.OlePropertyGet("ActiveWorkBook");
WorkSheet1=WorkBook1.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
//清空EXCEL表的数据
int nRow=WorkSheet1.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count");
int nCol=WorkSheet1.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count");
for(int i=5;i<=nRow+1;i++)
WorkSheet1.OlePropertyGet("Rows",i).OlePropertySet("Value","");
//给Excel表赋值
for(int j=1;j<10;j++)
{
for(int i=1;i<10; i++)
{
AnsiString StrValue=IntToStr(i*j);
WorkSheet1.OlePropertyGet("Cells",i+4,j).OlePropertySet("Value",StrValue.c_str());
}
}
AnsiString StrRange="A"+IntToStr(1)+":I"+IntToStr(nRow);//获取操作范围
Range=WorkSheet1.OlePropertyGet("Range",StrRange.c_str());
Borders=Range.OlePropertyGet("Borders");//获取边框对象
// Borders.OlePropertySet("linestyle",xlNone); //去掉边框线
//画表格边框
Borders.OlePropertySet("linestyle",xlContinuous);
Borders.OlePropertySet("weight",xlThin);
Borders.OlePropertySet("colorindex",xlAutomatic);
WorkBook1.OleProcedure("Save");//保存表格
WorkBook1.OleProcedure("Close");//关闭表格
ExcelApp.OleFunction("Quit");//退出Excel
char StrCurDir[MAX_PATH+1];
GetCurrentDirectory(MAX_PATH,StrCurDir);
char exeStr[]="Export.xls";
SHELLEXECUTEINFO Info;
memset (&Info, 0, sizeof(Info)) ;
Info.cbSize=sizeof (Info) ;
Info.lpVerb="open" ;
Info.lpFile=exeStr;
Info.lpParameters=NULL;
Info.fMask=SEE_MASK_NOCLOSEPROCESS ;
Info.nShow=SW_SHOWDEFAULT ;
if (! ShellExecuteEx (&Info))
MessageBox(Handle,"打开Excel文件Export.xls失败!","信息提示",MB_OK);
SetCurrentDirectory(StrCurDir);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -