给你点我以前写的程序,可参考看一下,我使用的是Delphi的Server组控件。
procedure TForm1.ConnectWord(Sender: TObject);
begin
try
Wordapplication.Connect;
except
MessageDlg('Word可能没有安装!', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := cbVisible.Checked;
WordApplication.Caption := 'Delphi automation';
end;
procedure TForm1.NewDocument(Sender: TObject);
var
Template,NewTemplate,ItemIndex:OleVariant;
begin
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
WordApplication.Documents.Add(Template, NewTemplate);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
WordDocument.PageSetup.PageHeight:=1190.55;
WordDocument.PageSetup.PageWidth:=841.9;
WordDocument.PageSetup.LinesPage:=42;
WordDocument.PageSetup.Orientation := wdOrientLandscape;
end;
procedure TForm1.AddPageHeader(Sender: TObject);
var
mmm,nnn,aaa,tit:OleVariant;
n:integer;
begin
mmm:=wdLine;
nnn:=1;
aaa:=wdFieldPage;
WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
WordApplication.Selection.Move(mmm,nnn);
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication.Selection.Range.InsertAfter(edtTit.text+#13);
// WordApplication.Selection.Move(mmm,nnn);
// WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
// WordApplication.Selection.Range.InsertAfter('地方(部门):'+ADODataSet1.FieldByName('地区名称').AsString+'('+ADODataSet1.FieldByName('地区编码').AsString+')');
WordApplication.Selection.Move(mmm,nnn);
// WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
// WordApplication.Selection.Range.InsertAfter('项目类型:'+ADODataSet1.FieldByName('项目类型').AsString);
// WordApplication.Selection.Range.InsertAfter('项目类型:其它');
// tit:='d:/火炬计划/'+ADODataSet1.FieldByName('地区名称').AsString+'.DOC';
tit:=extractfilepath(application.ExeName)+edtFilename.Text+'.DOC';
WordDocument.SaveAs(tit);
WordDocument.Tables.Add(WordApplication.Selection.Range,1,StrToInt(edtList.text));
for n:=1 to StrToInt(edtList.text)do
WordApplication.Selection.Tables.Item(DOCTables).Cell(1,n).Range.InsertAfter(ListBox1.Items.Strings[n-1]);
WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument
end;
procedure TForm1.AddPageFooter(Sender: TObject);
var
mmm,nnn,aaa:OleVariant;
begin
mmm:=wdLine;
nnn:=1;
aaa:=wdFieldPage;
WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageFooter;
WordApplication.Selection.Move(mmm,nnn);
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication.Selection.InsertAfter('第');
mmm:=wdCharacter;
WordApplication.Selection.Move(mmm,nnn);
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,aaa,mmm,nnn);
aaa:=wdFieldNumPages;
WordApplication.Selection.InsertAfter('页/第');
WordApplication.Selection.Move(mmm,nnn);
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,aaa,mmm,nnn);
WordApplication.Selection.InsertAfter('页');
WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument
end;
procedure TForm1.AddMainDocument(Sender: TObject);
var
m,n:integer;
tempstr:String;
aaa,mmm,nnn:OleVariant;
begin
if StrToInt(edtRow.text)<1 then
Exit;
n:=1;
m:=1;
mmm:=wdLine;
nnn:=1;
tempstr:='为空';
try
// WordApplication.Selection.Select;
WordApplication.Selection.InsertAfter('地方(部门):'+ADODataSet1.FieldByName('地区名称').AsString+'('+ADODataSet1.FieldByName('地区编码').AsString+')');
WordApplication.Selection.Move(mmm,nnn);
WordDocument.Tables.Add(WordApplication.Selection.Range,StrToInt(edtRow.text),StrToInt(edtList.text));
ADODataSet2.First;
while m<StrToInt(edtRow.text)+ndo
begin
if ADODataSet2.FieldByName('类型编码').AsString<>tempstr then
begin
WordDocument.Tables.Item(DOCTables).Cell(m,n).Select;
aaa:=1;
WordApplication.Selection.InsertRows(aaa);
WordApplication.Selection.Cells.Merge;
WordDocument.Tables.Item(DOCTables).Cell(m,1).Range.InsertAfter('地方(部门):'+ADODataSet1.FieldByName('地区名称').AsString+'('+ADODataSet1.FieldByName('地区编码').AsString+')'+' 项目名称:'+ADODataSet2.FieldByName('项目类型').AsString);
tempstr:=ADODataSet2.FieldByName('类型编码').AsString;
n:=n+1;
m:=m+1;
end;
WordDocument.Tables.Item(DOCTables).Cell(m,1).Range.InsertAfter(IntToStr(ADODataSet2.RecNo));
WordDocument.Tables.Item(DOCTables).Cell(m,2).Range.InsertAfter(ADODataSet2.FieldByName('计划项目编号').AsString);
WordDocument.Tables.Item(DOCTables).Cell(m,3).Range.InsertAfter(ADODataSet2.FieldByName('项目名称').AsString);
WordDocument.Tables.Item(DOCTables).Cell(m,4).Range.InsertAfter(ADODataSet2.FieldByName('主要内容和技术指标').AsString);
WordDocument.Tables.Item(DOCTables).Cell(m,5).Range.InsertAfter(ADODataSet2.FieldByName('承担单位').AsString);
WordDocument.Tables.Item(DOCTables).Cell(m,6).Range.InsertAfter(ADODataSet2.FieldByName('起止年限').AsString);
WordDocument.Tables.Item(DOCTables).Cell(m,7).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('合计').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,8).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('自筹').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,9).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('申请贷款').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,10).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('匹配').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,11).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('产值').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,12).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('交税总额').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,13).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('税后利润').AsFloat));
WordDocument.Tables.Item(DOCTables).Cell(m,14).Range.InsertAfter(FormatFloat('#.####',ADODataSet2.FieldByName('创汇').AsFloat));
ADODataSet2.Next;
m:=m+1;
end;
WordDocument.Tables.Item(DOCTables).Cell(m-1,15).Select;;
WordApplication.Selection.Move(mmm,nnn);
WordApplication.Selection.Move(mmm,nnn);
btnClose.Enabled := True;
except
on E: Exceptiondo
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
procedure TForm1.CloseWord(Sender: TObject);
var
SaveChanges,
OriginalFormat,
RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication.Disconnect;
btnClose.Enabled := False;
except
on E: Exceptiondo
begin
Showmessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;