1、先准备好WORD文档模板空表格。其表格如下
2、利用DELPHI程序自动填充WORD表格。并作文本捡替换操作。最后生成的效果如下图。
程序如下
1、先定义一个WINWORD单元,定义对类WINWORD.
unit WinWord;
{##################################################
单元名称:WinWord
功能: 控制WORD对象
程序设计:Armstronglou 技术支持:QQ:10458841
###################################################}
interface
uses
Variants, ComObj, windows, SysUtils;
Type
TWinWord = Class
private
// 注意表格的索引及单元格计数Tables.Item(1).Cell(行,列)是从1开始的
FReadOnly : Boolean;
FVisible : Boolean;
procedure SetReadOnly(Value: Boolean);
procedure SetVisible(value: Boolean);
public
WordApp,table: Variant;
constructor Create;
destructor Destory;
procedure CloseWord;
procedure OpenDoc(nDocFile:string);
procedure Save;
procedure SaveAs(filename:string);
procedure Quit;
procedure StrReplace(SearchStr, ReplaceStr:string;Range:Byte=2);
procedure StrTypeReplace(SearchStr, ReplaceStr: string);
procedure InsertRowsInTable(rowindex, rowcount: integer);
property Visible:Boolean Read FVisible Write SetVisible;
property DocReadOnly:Boolean Read FReadOnly Write SetReadOnly;
procedure SetCell(row,col:integer;value:string;tableindex:integer=1);
end;
implementation
const
wdFindContinue = 1;
wdReplaceOne = 1;
wdReplaceAll = 2;
{ TWinWord }
procedure TWinWord.CloseWord;
{关闭WORD}
begin
try
Wordapp.quit;
Except
MessageBox(0,'未找到WORD程序,您可能已经关闭了!', '提示', MB_ICONINFORMATION+MB_OK);
end;
Wordapp := unassigned;
end;
constructor TWinWord.Create;
begin
try
self.WordApp := CreateOleObject('Word.Application'); //WORD、WPS均可使用
//self.WordApp := CreateOleObject('KWps.Application'); //WPS 专用
self.FReadOnly := False;
except
MessageBox(0,'创建WORD对象出错了','错误',MB_ICONERROR + MB_OK);
end;
end;
destructor TWinWord.Destory;
begin
self.WordApp:=0;
end;
procedure TWinWord.InsertRowsInTable(rowindex, rowcount: integer);
//复制空行,rowindex选择用于复制的空行,rowcount复制行数
begin
//选中表格的cell(8,1) 行第8行第1列 复制第8行
WordApp.ActiveDocument.Tables.Item(1).Cell(rowindex,1).select;
WordApp.Selection.InsertRowsBelow(rowcount);
end;
procedure TWinWord.OpenDoc(nDocFile: string);
{打开WORD文档 }
begin
try
WordApp.Documents.Open(FileName := nDocfile, ReadOnly := False);
except
MessageBox(0,pchar('打开文件' + nDocFile + '出错!'),'错误',MB_ICONERROR + MB_OK + MB_SYSTEMMODAL);
end;
end;
procedure TWinWord.Quit; //退出WORD
begin
//不保存,直接退回
if MessageBox(0,'文件没有保存,确认要退出吗?','确认',MB_YESNO) = idYes Then
Wordapp.quit;
end;
procedure TWinWord.Save;
begin
//WordApp.ActiveDocument.SaveAs('ZJZL216-2021.docx');
WordApp.ActiveDocument.Save();
end;
procedure TWinWord.SaveAs(filename:string);
begin
WordApp.ActiveDocument.SaveAs(filename);
end;
procedure TWinWord.SetReadOnly( Value: Boolean);
begin
FReadOnly := Value;
end;
procedure TWinWord.SetVisible(value: Boolean); //设置WORD窗口的可见性
begin
self.FVisible := Value;
self.WordApp.Visible := Value;
end;
procedure TWinWord.StrReplace(SearchStr, ReplaceStr: string; Range:Byte=2);
//进行替换操作 范围: wdReplaceOne = 1; wdReplaceAll = 2;
begin
WordApp.Selection.Find.ClearFormatting;
WordApp.Selection.Find.Forward := True;
WordApp.Selection.Find.MatchAllWordForms := False;
WordApp.Selection.Find.MatchCase := False;
WordApp.Selection.Find.MatchWildcards := False;
WordApp.Selection.Find.MatchSoundsLike := False;
WordApp.Selection.Find.MatchWholeWord := False;
WordApp.Selection.Find.MatchFuzzy := False;
WordApp.Selection.Find.Wrap := wdFindContinue;
WordApp.Selection.Find.Format := False;
WordApp.Selection.Find.Text := SearchStr;
//执行替换 范围: wdReplaceOne = 1; wdReplaceAll = 2;
WordApp.Selection.Find.Replacement.Text := ReplaceStr;
WordApp.Selection.Find.Execute(Replace := Range)
end;
procedure TWinWord.StrTypeReplace(SearchStr, ReplaceStr: string);
{用输入文本的方式实现文本替换 本方法适当超过255个字符的长文本替换方法 }
var
st1:string;
begin
//因空字符用type方式不会进行替换,所以,替换为空格
st1 := ReplaceStr;
if st1='' then st1 := ' ';
WordApp.Selection.Find.ClearFormatting;
WordApp.Selection.Find.Forward := True;
WordApp.Selection.Find.MatchAllWordForms := False;
WordApp.Selection.Find.MatchCase := False;
WordApp.Selection.Find.MatchWildcards := False;
WordApp.Selection.Find.MatchSoundsLike := False;
WordApp.Selection.Find.MatchWholeWord := False;
WordApp.Selection.Find.MatchFuzzy := False;
WordApp.Selection.Find.Wrap := wdFindContinue;
WordApp.Selection.Find.Format := False;
WordApp.Selection.Find.Text := SearchStr;
//执行替换操作
WordApp.Selection.Find.Execute();
WordApp.Selection.TypeText(st1);
end;
procedure TWinWord.SetCell(row,col:integer; value:string; tableindex:integer=1);
// tableindex:表格索引号,从1开始;row,col:单元格行列索引,从1开始
var
i:integer;
begin
//表格序号,单元格序号从1开始
self.table := WordApp.ActiveDocument.Tables.Item(tableindex); //第一个表格
table.Cell(row, col).Range.InsertAfter('需要插入的文本内容');
end;
end.
2、调用方法:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
winword;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
winword1:Twinword;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button3Click(Sender: TObject);
var
docfile:string;
begin
docfile := 'C:\Users\Administrator\Documents\test01\mydoc.docx'; //WORD文档模板
self.Winword1 := TWinword.Create;
Winword1.DocReadOnly := True;
self.Winword1.Visible := True;
winword1.OpenDoc(docfile);
//MessageBox(0,'WORD文档对象已生成','消息',MB_ICONINFORMATION + MB_OK)
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
self.winword1.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//在第7行之后插入空行2行
self.Winword1.InsertRowsInTable(7,2);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
table:variant;
begin
table := winword1.WordApp.ActiveDocument.Tables.Item(1); //第一个表格
table.Cell(1, 2).Range.InsertAfter('1,2:宁波XX有限公司');
table.Cell(1, 4).Range.InsertAfter('1,4:李建设');
table.Cell(1, 6).Range.InsertAfter('1,6:电话');
table.Cell(2, 2).Range.InsertAfter('2,2:XX路1号');
table.Cell(2,4).Range.InsertAfter('2,4: 12@qq.com');
table.Cell(3, 2).Range.InsertAfter('3,2:受检单位');
table.Cell(3, 4).Range.InsertAfter('3,4:采样地址');
table.Cell(4, 2).Range.InsertAfter('4,2:项目名称');
table.Cell(4, 4).Range.InsertAfter('4,4:A-13');
end;
procedure TForm1.Button4Click(Sender: TObject);
var
I:integer;
table:variant;
begin
table := winword1.WordApp.ActiveDocument.Tables.Item(1); //第一个表格
for I:= 1 to 3 do
begin
table.Cell(I+6, 1).Range.InsertAfter(IntToStr(I+6));
table.Cell(I+6, 4).Range.InsertAfter('检测项目'+IntToStr(I));
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
//替换短文本 256个字符以内,超过长度可能会出错
self.winword1.StrReplace('{$WAY1}','报告出具方式1',);
//替换长文本
self.winword1.StrTypeReplace('{$WAY2}','报告出具方式2:1、第一,....。2、第二,....。');
end;
end.