Excel(word) 的简单应用 (100分)

  • 主题发起人 主题发起人 hzgood
  • 开始时间 开始时间
H

hzgood

Unregistered / Unconfirmed
GUEST, unregistred user!
EXCEL的打开,关闭;word的打开,关闭怎么写;
myexcel:olevariant;
....
myexcel.createoleobject;
myexcel.free
myexcel.application.visible:=true;
以上语句对吗?规范的怎么写?劳驾,劳驾
 
转载
启动word时用如下代码:

begin

try

wordapplication.connect;

except

messagedlg('word may not be installed', mterror, [mbok], 0);

abort;

end;

wordapplication.visible := true;

wordapplication.caption := 'delphi automation';

end;

关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:

var

savechanges, originalformat, routedocument: olevariant;

begin

savechanges := wddonotsavechanges;

originalformat := unassigned;

routedocument := unassigned;

try

wordapplication.quit(savechanges, originalformat, routedocument);

wordapplication.disconnect;

except

on e: exception do

begin

showmessage(e.message);

wordapplication.disconnect;

end;

end;

end;

让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:

var

itemindex :olevariant;

filename, confirmconversions, readonly, addtorecentfiles,

passworddocument, passwordtemplate, revert,

writepassworddocument, writepasswordtemplate, format: olevariant;

begin

if not dlgopen.execute then

exit;



{open document}

filename := dlgopen.filename;

confirmconversions := false;

readonly := false;

addtorecentfiles := false;

passworddocument := '';

passwordtemplate := '';

revert := true;

writepassworddocument := '';

writepasswordtemplate := '';

format := wdopenformatdocument;



wordapplication.documents.open( filename, confirmconversions,

readonly, addtorecentfiles, passworddocument, passwordtemplate,

revert, writepassworddocument, writepasswordtemplate, format );



{assign worddocument component}

itemindex := 1;

worddocument.connectto(wordapplication.documents.item(itemindex));



{turn spell checking of because it takes a long time if enabled and slows down winword}

wordapplication.options.checkspellingasyoutype := false;

wordapplication.options.checkgrammarasyoutype := false;

end;

让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>:

var

findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,

matchallwordforms, forward, wrap, format, replacewith, replace: olevariant;

begin

findtext := '<#name>';

matchcase := false;

matchwholeword := true;

matchwildcards := false;

matchsoundslike := false;

matchallwordforms := false;

forward := true;

wrap := wdfindcontinue;

format := false;

replacewith := 'delphi';

replace := true;

worddocument.range.find.execute( findtext, matchcase, matchwholeword,

matchwildcards, matchsoundslike, matchallwordforms, forward,

wrap, format, replacewith, replace );

end;

 
try //connect to Excel
ExcelOle_app := CreateOleObject('Excel.application');
except
application.MessageBox('请安装Micosoft Excel', '提示信息', MB_OK or mb_ICONINFORMATION);
Exit;
end;
try
ExcelOle_workbook := ExcelOle_app.workbooks.open(Form_main.Edit_ML.Text + form_main.Edit_WJ.Text);
except
application.MessageBox('报表样式文件有误!', '提示信息', MB_OK);
Abort;
end;
try
ExcelOle_WorkSheet := ExcelOle_app.activesheet;
ExcelOle_WorkSheet.visible:=true;
except
end;
这样就打开了Excel,就可以进一步操作了,打开word相似。再可以查查以前的帖子。
 
在use 中加入oleserver
調用excel
procedure TForm1.Button1Click(Sender: TObject);
MsExcel :Variant;
begin
try
MSExcel:=CreateOleObject('Excel.Application');
msexcel.visible:=true;
MSExcel.Workbooks.Add;
Msexcel.Workbooks[1].Worksheets[1].name:='測試數據';
insertdata;
except
Application.MessageBox('系統錯誤!','警告信息',mb_OK or mb_ICONSTOP);
Exit;
end;
end;

調用word
procedure TForm1.Button2Click(Sender: TObject);
var
MsWord :Variant;
begin
try
MSWord:=CreateOleObject('Word.Application');
msWord.visible:=true;
msword.documents.add;
except
Application.MessageBox('系統錯誤!','警告信息',mb_OK or mb_ICONSTOP);
Exit;
end;
end;

 
在interface中添加
uses comobj;
...
procedure tbutton1clicek(sendor.tobject);
var
myexcel:variant;
begin
myexcel:=createoleobject('excel.application');
myexcel.visible:=false;
...
//释放时用以下语句
myexcel:=unassigned;
end;
 
myexcel.quit不行嗎﹖
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
446
import
I
后退
顶部