问个问题20分奉上!! 1.我想在DELPHI中生成WORD文档,生成表格和文字, 实现打印WORD文档的功能,如何实现,我把文档放在什么地方. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Delphi+Word解决方案参考[转帖]这是我做项目过程中自己做的几个函数,见到大家都在问Word的问题。现在拿出来和大家共享。(希望有朋友可以进一步添加新的功能,或者做成包或者lib等,更方便大家使用。我自己是没有时间啦,呵呵)使用前,先根据需要建立一个空的WORD文件作为模板,在模板文件中设置好各种格式和文本。另外,其中的PrnWordTable的参数是TDBGridEh类型的控件,取自Ehlib2.6其中用到的shFileCopy函数(用于复制文件)和guiInfo函数(用于显示消息框)也是自己编写的,代码也附后。 示范代码如下: 代码完成的功能:1. 替换打印模板中的"#TITLE#"文本为"示范代码1"2. 并且将DBGridEh1控件当前显示的内容插入到文档的末尾3. 在文档末尾插入一个空行4. 在文档末尾插入新的一行文本5. 将文档中的空行去掉 if PrnWordBegin('C:\打印模板.DOC','C:\目标文件1.DOC') then begin PrnWordReplace('#TITLE#','示范代码1'); PrnWordTable(DBGridEh1); PrnWordInsert(''); PrnWordInsert('这是新的一行文本'); PrnWordReplace('^p^p','^p',true); PrnWordSave; end; 源代码如下: //Word打印(声明部分) wDoc,wApp:Variant; 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;overload; 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;begin result:=false; //复制模版 if tempDoc<>'' then if not shFileCopy(tempDoc,docName) then exit; //连接Word try wApp:=CreateOleObject('Word.Application'); except guiInfo('请先安装 Microsoft Word 。'); exit; end; try //打开 if tempDoc='' then begin //创建新文档 wDoc:=wApp.Document.Add; wDoc.SaveAs(docName); end else begin //打开模版 wDoc:=wApp.Documents.Open(docName); end; except guiInfo('打开模版失败,请检查模版是否正确。'); 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;begin if bSimpleReplace then begin //简单处理,直接执行替换操作 try wApp.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; 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:=wdReplaceAll); result:=true; except result:=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 := wdFindContinue; 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 := False; wApp.Selection.Find.Execute; wApp.Selection.MoveRight(wdCharacter,1); //开始逐行插入 for i:=0 to reWord.Lines.Count-1 Do begin //插入当前行 wApp.Selection.InsertAfter(reWord.Lines[i]); //除最后一行外,自动加入新行 if i<reWord.Lines.Count-1 then wApp.Selection.InsertAfter(#13); end; //删除替换位标 wApp.Selection.Find.ClearFormatting; wApp.Selection.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.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:=wdReplaceAll); result:=true; except result:=false; end;end; dbgrideh有个dghshowrecno可以显示行号,但怎样用printdbgrideh打印出来? createwindow 出现找不到元素 求教一下有关SMS方面问题 在线等,QuickRep中要输出竖列的文字怎么做? 请教大虾一个问题 adoquery问题,高手请进 谁知道dxExpress那套控件的汉化包在哪儿能下载到?多提供网址多给分 请教有点难的问题,谢谢,关于时时数据插入数据库,谢谢,急,下班前给分, 如何使状态栏中的图标闪烁 再谈多态--多态的应用举例: 提问有关数据库的问题 如何让鼠标移到edit上时,edit里面的里面被选种?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货