全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2479
推到 Plurk!
推到 Facebook!

怎樣用TExcelApplication控制項打開一個excel文檔

尚未結案
jeffen
一般會員


發表:7
回覆:2
積分:1
註冊:2003-10-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-10-15 11:02:04 IP:218.5.xxx.xxx 未訂閱
使用TExcelApplication控制項打開一個excel文檔碰到問題,使用語句如下: ExcelApplication1->Workbooks->Open(“e:\test.xls”,TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),0); 系統提示:不能轉化AnsiString to wchar_t *; 我於是先設置變數wchar_t* filename = L”e:\test.xls”;再代入上面的語句,編譯通過,運行提示錯誤 (this->Open(Filename, updatelinks, readonly, format, password, writerespassword, ignorereadonlyRecommended, Origin, Delimiter, Editable, Notify,Converter,AddToMru, lcid,(Excel_2k::ExcelWorkbook**)&RHS))Error:c0000005(-1073741819)……錯誤,不知道什麼原因,大部分網站只有delphi的例子,哎…… 另外不知道誰有使用c bulder的控制項進行office操作的例子的,望共用之…… 皆为我师 發表人 - Gemi0305 於 2003/10/15 11:08:32
------
皆为我师
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-10-15 11:22:44 IP:61.218.xxx.xxx 未訂閱
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
系統時間:2024-05-08 11:12:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!