📄 mainform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Mainform.h"
#pragma link "Excel_2K_SRVR"
#include "Excel_2K_SRVR.h"
#include <OleServer.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
AnsiString StrFileName=GetCurrentDir()+"\\Import.xls";
if(!FileExists(StrFileName))
{
MessageBox(Handle,"Excel表文件Import.xls不存在,无法打开!","信息提示",MB_ICONSTOP|MB_OK);
return;
}
Variant ExcelApp; //对象
Variant WorkBook1; //工作薄
Variant WorkSheet1; //工作表
try
{
ExcelApp=Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(Handle,"无法启动Excel!","信息提示",MB_ICONSTOP|MB_OK);
return;
}
ExcelApp.OlePropertySet("Visible",true);//设置Excel为可见
//打开指定的Excel文件,文件中最好只设定一个Sheet
ExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open",StrFileName.c_str());
WorkBook1=ExcelApp.OlePropertyGet("ActiveWorkBook");
WorkSheet1=WorkBook1.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
if(MessageBox(Handle,"是否将当前的EXCEL文件导入程序?","信息提示",MB_YESNO)==IDNO)
return;
int nRow=WorkSheet1.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count");
int nCol=WorkSheet1.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count");
//从Excel表获取数据
for(int i=5;i<=nRow;i++)
{
for(int j=1;j<=nCol;j++)
{
AnsiString Str;
Str=WorkSheet1.OlePropertyGet("Cells",i,j).OlePropertyGet("Value");
this->StringGrid1->Cells[i-5][j-1]=Str;
}
}
WorkBook1.OleProcedure("Close");//关闭表格
ExcelApp.OleFunction("Quit");//退出Excel
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -