用WORD方式 先建立一个Word文档在其中设计一个表格,存为模板
procedure TForm_main.wsprinttoword;
var
Template,NewTemplate,ItemIndex,TempName,numrows:OleVariant;
CurrentAppDir:string;
wordtable:table;
i:integer;
begin
CurrentAppDir:=GetCurrentDir;
TempName:=CurrentAppDir+'/归档文件.dot';
Template:=TempName;
ItemIndex:=1;
try
try
WordApplication1.Connect;
except
MessageDlg('您的系统没有安装Word!', mtError,[mbOk], 0);
Abort;
end;
WordApplication1.Visible:=True;
WordApplication1.Caption:='归档文件打印输出预览';
NewTempLate:=false;
WordApplication1.Documents.Add(Template,NewTemplate);
//生成新文档
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));
WordApplication1.Options.CheckSpellingAsYouType:=False;//关闭拼写检查
WordApplication1.Options.CheckGrammarAsYouType:=False;//关闭语法检查
DBEdit_temp.DataField:='f_type';
DBEdit_temp.DataSource:=DataSource_wsprint;
Client_wsprint.Active:=true;
Client_wsprint.First;
wordtable:=WordDocument1.Tables.Item(1);//item(1) is word 中的第一个表格
wordtable.Cell(4,1).Select ;
numrows:=Client_wsprint.RecordCount-1;
//插入一行
WordApplication1.Selection.InsertRows(numrows);
i:=0;
DBEdit_temp.DataField:='f_type';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(2,3).Select ;
WordApplication1.Selection.paste;
end;
while not Client_wsprint.eofdo
begin
DBEdit_temp.DataField:='f_bh';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,1).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='f_zrz';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,2).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='f_wno';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,3).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='f_title';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,4).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='f_date';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,5).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='F_Dageno';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,6).Select ;
WordApplication1.Selection.paste;
end;
DBEdit_temp.DataField:='D_bgqx';
if trim(DBEdit_temp.text)<>'' then
begin
DBEdit_temp.SelectAll;
DBEdit_temp.CopyToClipboard;
wordtable.Cell(4+i,7).Select ;
WordApplication1.Selection.paste;
end;
Client_wsprint.Next;
i:=i+1;
end;
WordApplication1.Disconnect;
except
on E:Exceptiondo
begin
ShowMessage(E.Message);
WordApplication1.Disconnect;
end;
end;
注意Word不同的版本可能有不同的表现,单元格排列命名方式不同,其实用Word不是好方法
我现在基本不用,还是FastReport好用