本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
================

注会练习软件
http://www.cpasoft.com.cn
我的注会软件官网

http://blog.163.com/abc7105@126/

 

 


哈哈,热爱快“过气”的DELPHI


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:注册会计师(注会)练习软件
日志总数:398
评论数量:116
留言数量:27
访问次数:3273740
建立时间:2005年6月6日




[delpih编程]【转】delpih 生成word文档
软件技术

吕向阳 发表于 2009/4/2 13:58:28

最近帮同学用Delphi 7写的一个数据库应用中的一个功能是将查询的结 果导出到一个Word文档中保存。虽然Delphi 7的Servers面板中提供了 TWordApplication和TWordDocument组件,但是帮助中却几乎没有写它 们的使用方法。于是在中国期刊网上down了许多的相关文章来看,只可 惜几乎都是用Delphi 5写的(Delphi 7中不能兼容),而且都只是简单 的介绍了一下,甚是郁闷。在经过一天的摸索之后终于用Delphi 7实现 了这个功能。代码如下: //uses Word2000, ComObj; //WordApp: TWordApplication; //WordDoc: TWordDocument; procedure TfrmDetails.btnExportClick(Sender: TObject); //单击 “导出“按钮 var V:Variant; Template,NewTemplate,DocumentType,Visible:OleVariant; itemIndex:OleVariant; fileName:Olevariant; NoPrompt,OriginalFormat:OleVariant; RouteDocument,SaveChanges:OleVariant; begin //指定文档的路径和文件名 fileName:='C:\LogAdmin\doc\'+'值班日志'+Trim (DBTextID.Caption)+'.doc'; //如果该日志的对应Word文档已经存在则提示是否覆盖 if FileExists(fileName)=true then begin Beep; if Application.MessageBox('文档已经存在,是否覆盖?','警 告',MB_OKCANCEL)=IDCANCEL then Abort; end; //测试当前是否运行了Word 2000 try V:=GetActiveOleObject('Word.Application'); except //未运行则运行之 V:=CreateOleObject('Word.Basic'); end; try //连接到Word 2000 WordApp.Connect; except Beep; MessageDlg('不能生成文档,请确认是否安装了Word 2000!',mtError,[mbOK],0); Abort; end; //显示Word 2000 WordApp.Visible:=true; //给调用Add函数使用的实参赋值 Template:=EmptyParam; NewTemplate:=False; DocumentType:=wdNewBlankDocument; Visible:=true; //调用Add函数 WordApp.Documents.Add (Template,NewTemplate,DocumentType,Visible); //连接到新建的文档 itemIndex:=1; WordDoc.ConnectTo(WordApp.Documents.Item(itemIndex)); //文档另存为 WordDoc.SaveAs(fileName); //开始向Word文档中写入内容 with WordApp.Selection do begin Font.Size:=20; Font.Bold:=2; Paragraphs.Alignment:=wdAlignParagraphCenter; TypeText('值班日志详细内容'); TypeParagraph; //换行 TypeParagraph; Font.Size:=12; Font.Bold:=0; Paragraphs.Alignment:=wdAlignParagraphLeft; TypeText('编号: '+DBTextID.Caption); TypeParagraph; TypeText('日期: '+DBTextDate.Caption); TypeParagraph; TypeText('温度: '+DBTextT.Caption); TypeParagraph; TypeText('湿度: '+DBTextH.Caption); TypeParagraph; TypeText('天气: '+DBTextWeather.Caption); TypeParagraph; TypeText('值班人: '+DBTextName.Caption); TypeParagraph; TypeText('值班时间:'+DBTextTime.Caption); TypeParagraph; TypeText('有无异常:'+lbException.Caption); TypeParagraph; TypeText('使用工具:'); TypeParagraph; TypeText(DBMemoTool.Text); TypeParagraph; TypeText('现场环境:'); TypeParagraph; TypeText(DBMemoEnv.Text); TypeParagraph; TypeText('记录一: '); TypeParagraph; TypeText(DBMemoR1.Text); TypeParagraph; TypeText('记录二: '); TypeParagraph; TypeText(DBMemoR2.Text); TypeParagraph; TypeText('记录三: '); TypeParagraph; TypeText(DBMemoR3.Text); TypeParagraph; TypeText('备注: '); TypeParagraph; TypeText(DBMemoMemo.Text); TypeParagraph; end; //保存文档 NoPrompt:=false; OriginalFormat:=wdOriginalDocumentFormat; WordApp.Documents.Save(NoPrompt,OriginalFormat); //关闭文档 SaveChanges:=wdSaveChanges; OriginalFormat:=wdOriginalDocumentFormat; RouteDocument:=false; WordApp.Documents.Close (SaveChanges,OriginalFormat,RouteDocument); //断开和Word 2000的连接 WordApp.Disconnect; MessageDlg('日志内容导出成功!保存 为'+fileName,mtInformation,[mbOK],0); //关闭窗体 frmDetails.Close; end;


阅读全文(1073) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144802093 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号