和word的兼容?(100分)

  • 主题发起人 主题发起人 the_lover
  • 开始时间 开始时间
T

the_lover

Unregistered / Unconfirmed
GUEST, unregistred user!
是这样的,有一些word文档,它们的很多的数据一样,希望能通过编程产生word文档,
把数据写入.
总的来说就如如何随心所欲的成生word文档,也有格式?
 
delphi
的自动控件
 
楼上说得对,用OLE是唯一保证兼容的方式了。
好象是D4吧,开始就有WORD的自动组件了,
先安装OFFICE,再安装DELPHI,安装完就有了。
 
下面的代码可以生成一个新的word文档或打开一个word文档,至于怎么用程序操作,
可以看看VBA帮助。
var
MSWord:olevariant;
begin
try
MSWord := CreateOLEObject('Word.Application');
except
ShowMessage('启动Word失败!请确认Word已正确安装');
exit;
end;
//MSWord.Documents.Open (FileName:='e:/newword.doc', ReadOnly:=True);//打开已存在的word文档
MSWord.Documents.Add; //创建一个新word文档
MSWord.Visible := 1;
end;
 
var Template,NewTemplate,ItemIndex:OleVariant;
coli,rowi:integer;
procedure setfont;
begin
WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
if ChkBoxUnderline.checked then WordFont.Underline := 2;
if ChkBoxBold.checked then WordFont.Bold := 1;
if ChkBoxItalic.Checked then WordFont.Italic := 1;
if ChkBoxEmboss.Checked then WordFont.Emboss := 1;
if ChkBoxEngrave.checked then WordFont.Engrave := 1;
if ChkBoxShadow.checked then WordFont.shadow := 1;
if ChkBoxDoublestrike.checked then WordFont.DoubleStrikeThrough := 1;
if ChkBoxStrike.checked then WordFont.StrikeThrough := 1;
WordFont.Size := StrToInt(Size.text);
if Fonttype.Itemindex >= 0 then
WordFont.Name := FontType.Items[FontType.Itemindex];
end;

begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
{Create new document}
Template := EmptyParam;
NewTemplate := False;
if ChkBoxNewDoc.Checked then
begin
WordApplication.Documents.Add(Template, NewTemplate);
{Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
end;
{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}
DBImgFishImg.CopyToClipboard;
WordDocument.Sentences.Last.Paste;
WordDocument.Range.InsertAfter('Common Name: ' + Table.Fields.Fields[2].AsString + #13);
SetFont;
WordDocument.Range.InsertAfter('Species Name:' + Table.Fields.Fields[3].AsString + #13);
WordDocument.Range.InsertAfter('Length: ' + Table.Fields.Fields[4].AsString + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
with WordDocument do
begin
tables.Add(WordDocument.Words.Last,self.StringGrid1.RowCount,self.StringGrid1.colCount);
for coli:=0 to self.StringGrid1.colCount-1 do
for rowi:=0 to self.StringGrid1.RowCount-1 do
tables.Item(1).Cell(rowi+1,coli+1).Range.Set_Text(self.StringGrid1.Cells[coli,rowi]);
end;
//WordDocument.Range.PageSetup.
WordDocument.Range.PageSetup.Set_BottomMargin(100);
WordDocument.Range.InsertAfter('asbrdqwuier');

BtnCloseWord.Enabled := True;
BtnPrint.Enabled := True;
BtnPreview.Enabled := True;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
运行一下就看到效果了
 
用vba程序
 
还有一个方法,就是用一个RICHEDIT控件,将其PLAINTEXT设为默认的FALSE,并可根据
需要将其VISIBLE属性设为FALSE,将后将要放入WORD文档的文件逐段用
Richedit1.Lines.Add(文本)方法加入到RICHEDIT中,然后用
Richedit1.Lines.SaveToFile(路径/文件名.Rtf)方法保存到指定文件,这样就可以用
Word将其打开了,当然你可以在Word中将其另存为Doc文档。
 
多人接受答案了。
 
后退
顶部