Jeffen 您好: 參考看看
作者及資料來源已不可考 ExcelApplication 元件應用
-------------------------------------------------------------------------------- #include
#pragma hdrstop
//---------------------------------------------------------headers BC 5
#include //ffblk
#include //sprintf
#include //variant et olevariant
//--------------------------------------------------------------------------- #include "excelctrl1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Excel_2K_SRVR"
#pragma resource "*.dfm"
TFormExcel2k *FormExcel2k;
bool excel2k;
//---------------------------------------------------------------------------
__fastcall TFormExcel2k::TFormExcel2k(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormExcel2k::FormCreate(TObject *Sender)
{
excel2k=false; //a private bool to check if Excel is already opened
}
//--------------------------------------------------------------------------- void __fastcall TFormExcel2k::ButOpenClick(TObject *Sender)
{
if (excel2k)
return;
//
int i,nbSheet;
char s[257];
bool okWb;
//Variables for Excel
Excel_2k::WorkbooksPtr books;
Excel_2k::SheetsPtr sheets;
Excel_2k::RangePtr cells;
_WorkbookPtr wb; //ExcelWorkbookPtr wb;
_WorksheetPtr ws; //ExcelWorksheetPtr ws;
TVariant wbName,
wsName;
TVariantInParam readOnly, //bool
numSheet,intValue, //int
Cell1,Cell2,strValue; //string //opening ExcelApplication
try {
ExcelApplication->Connect(); //okXls
books=ExcelApplication->Get_Workbooks(); //okXls
ExcelApplication->Set_Visible(TDefLCID(),true); //okXls
excel2k=true;
}
catch (Exception &exception) {
Application->MessageBox(
"This application needs Excel 2k or more",
"Sorry...",
MB_ICONSTOP);
abort;
} //Opening a Workbook in Excel strcpy(s,"c:\\tmp\\toto.xls");
okWb=_ChkFile(s); //returns true if the above file already exists
wbName=s;
try {
if (okWb) {
//Open the existing Excel Workbook
//following line to open with defaults values
wb=books->Open(wbName);
//following 2 lines to show how to proceed with others values, readOnly for instance
// readOnly=true;
// wb=books->Open(wbName,TNoParam(),readOnly);
}
else {
//Or create a new Excel Workbook
wb=books->Add();
wb->SaveAs(wbName,TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),
xlNoChange,TNoParam(),TNoParam(),TNoParam(),TNoParam(),TDefLCID());
}
//Give a name to the Excel main windows, for instance the path of the Workbook
ExcelApplication->Caption=wbName;
//or explicitely
// ExcelApplication->Caption=TVariant("Toto"); //Recover the collection of Worksheets
sheets=wb->get_Sheets();
//and give each Worksheet a name
nbSheet=sheets->Count;
for (i=1;i<=nbSheet;i ) { // Worksheets are indexed from 1, not 0
sprintf(s,"Toto %d",i);
wsName=s;
numSheet=i;
ws=sheets->get_Item(numSheet);
ws->set_Name(wsName);
}
//to print all the sheets at the same time, try something like the following paragraph
// if (sheets) {
// sheets->PrintOut(1,10,
// TNoParam(),
// TNoParam(),
// TNoParam(),
// TNoParam(),
// TNoParam(),
// TDefLCID());
// } //Activate the 2nd Worksheet
numSheet=2;
ws=sheets->get_Item(numSheet);
ws->Activate(TDefLCID());
//select range of cells A1:C3
Cell1="A1";
Cell2="K27";
cells=ws->get_Range(Cell1,Cell2);
cells->Select(); //note : get_Range Select to select a range
//====================My test===========================
cells->Copy(); //this is Copy to Clipboard
//====================My test end======================= Cell1="B2";
cells=ws->get_Range(Cell1,TNoParam());
cells->Activate(); //note : get_Range Activate to activate a cell
//Put an int in the activated cell
intValue=1234;
cells->set_Value(intValue);
//or try the following for a string
// strValue="Hi, Toto !";
// cells->set_Value(strValue);
}
catch (Exception &exception) {
Application->MessageBox("Sure, there remains a problem anywhere !","Sorry...",MB_ICONSTOP);
abort;
}
}
//--------------------------------------------------------------------------- void __fastcall TFormExcel2k::ButCloseClick(TObject *Sender)
{
if (excel2k)
{
ExcelApplication->Workbooks->Close();
ExcelApplication->Disconnect();
excel2k=false;
} }
//---------------------------------------------------------------------------
void __fastcall TFormExcel2k::ButExitClick(TObject *Sender)
{
ModalResult=mrOk;
}
//---------------------------------------------------------------------------
void __fastcall TFormExcel2k::FormDestroy(TObject *Sender)
{
ButClose->Click(); //don't let Excel opened when leaving }
//--------------------------------------------------------------------------- bool __fastcall TFormExcel2k::_ChkFile(char *s)
{
ffblk bloc;
return (findfirst(s,&bloc,FA_NORMAL)==0);
}
//---------------------------------------------------------------------------
/*生活是一種藝術,用心生活才能享受生活*/
發表人 - axsoft 於 2003/10/15 11:26:53