在窗体上丢个frRTFExport控件. 在打印预览时就可以另存为rtf文件了(word能打开的) 用wordapplication 做了一个word报表,调试成功了,简单复制格式如下。我 加了不少的中文说明,大家可以参考或批评指正 当然大家也可以参考bbs的5532的unitword单元代码 例子: 在server中加入 wordapplication(word服务器控件),worddocument(word 文档) 和wordfont(设word字体的控件) 和一个 dbimage(将用它放图片到word中) procedure TFormcx1.btnwordClick(Sender: TObject); //新建word并填入信息 var i,j:integer;Template,NewTemplate,wdDocumentType,wdVisible ,ItemIndex:OleVariant; FileName, ConfirmConversions, count, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format :olevariant; procedure setfont(size,bold,underline:integer); //设字体的过程 begin WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font); WordFont.Size := size;//24; WordFont.Bold := bold;//1; WordFont.Underline := underline;//2 //也可以设置字体的其它属性: //WordFont.Italic := 0;//1; // WordFont.Emboss := 1; // WordFont.Engrave :=0;// 1; //WordFont.shadow :=0;// 1; //WordFont.DoubleStrikeThrough := 1; // WordFont.StrikeThrough := 0;//1; // WordFont.Name := '宋体 '; end; begin try Template := EmptyParam;//设定新建的word所使用的模板 NewTemplate := True; ItemIndex := 1;//设定文档号 try Wordapplication.Connect;//呼叫word服务 except MessageDlg( '也许word没有安装,请重试 ', mtError, [mbOk], 0); Abort; end; Wordapplication.Visible := True;//设新建的word文档为可见 WordApplication.Caption := 'Delphi automation ';//设新建的word文档的名称 {Create new document} Template := EmptyParam; NewTemplate := False; wdDocumentType := $00000000; // $00000001; wdVisible := true; //创建新的word文档,如下 WordApplication.Documents.Add(Template, NewTemplate); //************************* //注:如果要打开已经有的word文件,则如下代码, {Open document} //有时候 我喜欢用(**)表明为注释而不是{}因为{}有时用作编译开关和窗体声明 ,有时有歧义 (*格式:WordDocuments.Open(FileName,ConfirmConversions,ReadOnly,PassWordDoc ument, PasswordTemplate,Revent,WritePasswordDocument,WritePassWordTemplate, Format,Encoding,Visible) FileName: 文档名(包含路径) Confirmconversions: 是否显示文件转换对话框 ReadOnly: 是否以只读方式打开文档 AddToRecentFiles: 是否将文件添加到 "文件 "菜单底部的最近使用文件列表 中 PassWordDocument: 打开此文档时所需要的密码 PasswordTemplate: 打开此模板时所需要的密码 Revert: 如果文档已经,是否重新打开文档 WritePasswordDocument: 保存对文档更改时所需要的密码 WritePasswordTemplate: 保存对模板进行更改时所需要的密码 Format: 打开文档时所需使用的文件转换器 Encoding: 所使用的文档代码页 Visible: 打开文档的窗口是否可见 *) (*例: FileName := ExtractFiledir(Application.Exename)+ '/中科院项目书.doc '; 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} //连接word里新建的第一个document文档 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; //.......... //************************* //给文档添加内容{Insert data} //******************** //插入图片 DBImage1.CopyToClipboard;//把dbimage1控件里的图拷贝到粘贴板 WordDocument.Sentences.Last.Paste;//把粘贴板的内容贴到word文末 //插入文字 with WordDocument.Range do begin SetFont(20,1,0);//设字体为有下划线的20号的黑体字 InsertAfter( ' 项目编号: ' ); insertAfter( '00000001 '+#13); InsertAfter( ' ' + #13);//插入换行符号 InsertAfter( ' ' + #13); SetFont(30,1,1); InsertAfter( ' 中科院项目书 ' + #13); SetFont(20,1,0); InsertAfter( ' 类 别:重点计划 ' ); InsertAfter( '项目名称 ' ); InsertAfter( ' ' + #13#10); //略一部分 //插入表格 //插入5行5列的表一 WordDocument.Tables.Add(WordDocument.Sentences.Last ,5,5 ) ; with WordDocument.Tables.Item(1) do begin cell(1,1).range.text:= '项目名称 '; cell(1,2).range.text:= '单位情况 '; cell(1,3).range.text:= '申请人 '; cell(1,4).range.text:= '电话 '; cell(1,5).range.text:= '签字 '; end;//with WordDocument.Tables.Item(1) InsertAfter( ' ' + #13); setfont(20,1,0); InsertAfter( ' ' + #13#10); //插入表2 WordDocument.Tables.Add(WordDocument.Sentences.Last,1,1) ; InsertAfter( ' ' + #13#10); with WordDocument.Tables.Item(2) do begin cell(1,1).range.text:= '我要省略啦 ';//...略 end;// with WordDocument.Tables.Item(2) end;//with WordDocument.Range //通知服务结束,可以中断连接 WordApplication.Disconnect; except//如果word的服务有问题则跳出并弹出异常消息 on E: Exception do begin ShowMessage(E.Message); WordApplication.Disconnect; end; end;//try end; procedure TForm1.closewordClick(Sender: TObject); //关闭word 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; |
|