Delphi+Word解决方案参考.doc

上传人:博****1 文档编号:543911518 上传时间:2022-10-30 格式:DOC 页数:12 大小:83.01KB
返回 下载 相关 举报
Delphi+Word解决方案参考.doc_第1页
第1页 / 共12页
Delphi+Word解决方案参考.doc_第2页
第2页 / 共12页
Delphi+Word解决方案参考.doc_第3页
第3页 / 共12页
Delphi+Word解决方案参考.doc_第4页
第4页 / 共12页
Delphi+Word解决方案参考.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《Delphi+Word解决方案参考.doc》由会员分享,可在线阅读,更多相关《Delphi+Word解决方案参考.doc(12页珍藏版)》请在金锄头文库上搜索。

1、DelphiWord解决方案参考这是我做项目过程中自己做的几个函数,见到大家都在问Word的问题。现在拿出来和大家共享。(希望有朋友可以进一步添加新的功能,或者做成包或者lib等,更方便大家使用。我自己是没有时间啦,呵呵) 使用前,先根据需要建立一个空的WORD文件作为模板,在模板文件中设置好各种格式和文本。另外,其中的PrnWordTable的参数是TDBGridEh类型的控件,取自Ehlib2.6其中用到的shFileCopy函数(用于复制文件)和guiInfo函数(用于显示消息框)也是自己编写的,代码也附后。 示范代码如下: 代码完成的功能:1. 替换打印模板中的“#TITLE#”文本为

2、“示范代码1”2. 并且将DBGridEh1控件当前显示的内容插入到文档的末尾3. 在文档末尾插入一个空行4. 在文档末尾插入新的一行文本5. 将文档中的空行去掉 if PrnWordBegin(C:打印模板.DOC,C:目标文件1.DOC) thenbeginPrnWordReplace(#TITLE#,示范代码1);PrnWordTable(DBGridEh1);PrnWordInsert();PrnWordInsert(这是新的一行文本);PrnWordReplace(pp,p,true);PrnWordSave;end; 源代码如下: /Word打印(声明部分) wDoc,wApp:V

3、ariant;function PrnWordBegin(tempDoc,docName:String):boolean;function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;function PrnWordInsert(lineText:String;bNewLine:boolean=true):boolean;overload;function PrnWordInsert(var imgInsert:TImage;sBookMark:String=):boolean;over

4、load;function PrnWordInsert(var chartInsert:TChart;sBookMark:String=):boolean;overload;function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=):boolean;procedure PrnWordSave;procedure PrnWordEnd; /Word打印(实现部分) 功能:基于模板文件tempDoc新建目标文件docName并打开文件function PrnWordBegin(tempDoc,docName:String):boolean;

5、beginresult:=false;/复制模版if tempDoc thenif not shFileCopy(tempDoc,docName) then exit;/连接WordtrywApp:=CreateOleObject(Word.Application);exceptguiInfo(请先安装 Microsoft Word 。);exit;end;try/打开if tempDoc= thenbegin/创建新文档wDoc:=wApp.documentAdd;wDoc.SaveAs(docName);end else begin/打开模版wDoc:=wApp.document.Open

6、(docName);end;exceptguiInfo(打开模版失败,请检查模版是否正确。);wApp.Quit;exit;end;wApp.Visible:=true;result:=true;end; 功能:使用newText替换docText内容bSimpleReplace:true时仅做简单的替换,false时对新文本进行换行处理function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;var i:Integer;beginif bSimpleReplace thenbegi

7、n/简单处理,直接执行替换操作trywApp.Selection.Find.ClearFormatting;wApp.Selection.Find.Replacement.ClearFormatting;wApp.Selection.Find.Text := docText;wApp.Selection.Find.Replacement.Text :=newText;wApp.Selection.Find.Forward := True;wApp.Selection.Find.Wrap := wdFindContinue;wApp.Selection.Find.Format := False;

8、wApp.Selection.Find.MatchCase := False;wApp.Selection.Find.MatchWholeWord := true;wApp.Selection.Find.MatchByte := True;wApp.Selection.Find.MatchWildcards := False;wApp.Selection.Find.MatchSoundsLike := False;wApp.Selection.Find.MatchAllWordForms := False;wApp.Selection.Find.Execute(Replace:=wdRepla

9、ceAll);result:=true;exceptresult:=false;end;exit;end;/自动分行reWord.Lines.Clear;reWord.Lines.Add(newText);try/定位到要替换的位置的后面wApp.Selection.Find.ClearFormatting;wApp.Selection.Find.Text := docText;wApp.Selection.Find.Replacement.Text := ;wApp.Selection.Find.Forward := True;wApp.Selection.Find.Wrap := wdFi

10、ndContinue;wApp.Selection.Find.Format := False;wApp.Selection.Find.MatchCase := False;wApp.Selection.Find.MatchWholeWord := False;wApp.Selection.Find.MatchByte := True;wApp.Selection.Find.MatchWildcards := False;wApp.Selection.Find.MatchSoundsLike := False;wApp.Selection.Find.MatchAllWordForms := Fa

11、lse;wApp.Selection.Find.Execute;wApp.Selection.MoveRight(wdCharacter,1);/开始逐行插入for i:=0 to reWord.Lines.Count-1 Dobegin/插入当前行wApp.Selection.InsertAfter(reWord.Linesi);/除最后一行外,自动加入新行if ireWord.Lines.Count-1 thenwApp.Selection.InsertAfter(#13);end;/删除替换位标wApp.Selection.Find.ClearFormatting;wApp.Select

12、ion.Find.Replacement.ClearFormatting;wApp.Selection.Find.Text := docText;wApp.Selection.Find.Replacement.Text := ;wApp.Selection.Find.Forward := True;wApp.Selection.Find.Wrap := wdFindContinue;wApp.Selection.Find.Format := False;wApp.Selection.Find.MatchCase := False;wApp.Selection.Find.MatchWholeWo

13、rd := true;wApp.Selection.Find.MatchByte := True;wApp.Selection.Find.MatchWildcards := False;wApp.Selection.Find.MatchSoundsLike := False;wApp.Selection.Find.MatchAllWordForms := False;wApp.Selection.Find.Execute(Replace:=wdReplaceAll);result:=true;exceptresult:=false;end;end; 功能:打印TDBGridEh当前显示的内容基

14、于TDBGridEh控件的格式和内容,自动在文档中的sBookMark书签处生成Word表格目前能够支持单元格对齐、多行标题(两行)、底部合计等特性sBookMark:Word中要插入表格的书签名称function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=):boolean;var iCol,iLine,i,j,k:Integer;wTable,wRange:Variant;iRangeEnd:longint;iGridLine,iTitleLine:Integer;getTextText:String;getTextDisplay:bo

15、olean;titleList:TStringList;titleSplit,titleCol:Integer;lastTitleSplit,SubTitle:Integer;lastTitle:String;beginresult:=false;try/计算表格的列数(不包括隐藏的列)iTitleLine:=1; /始终默认为1iCol:=0;for i:=0 to dbG.Columns.Count-1 Dobeginif dbG.Columnsi.Visible thenbeginiCol:=iCol+1;end;end;/计算表格的行数(不包括隐藏的列)if dbG.DataSource.DataSet.Active theniLine:=dbG.DataSource.DataSet.Recor

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号