用Word实现报表套打的方法 (0分)

X

xmusyh

Unregistered / Unconfirmed
GUEST, unregistred user!
用Word实现报表套打的方法
我第一次接触报表套打时,也是感到非常的困惑,后来在大富翁论坛看了许多关于用Delphi操作Word的文章,
自己尝试成功后,也把自己的体会写下来.
具体的代码如下:
1.定义报表生成过程
Var WordApplication:TWordApplication;
WordDocument:TwordDocument;
//在WordDocument中,用 D 字符串替换所有的 S 字符串
procedure ReplaceString(S:string;D:String;WordDocument:TWordDocument);
var FindText,MatchCase,MatchWholeWord,MatchWildcards,
MatchSoundsLike,MatchAllWordForms,
Forward,Wrap,Format,ReplaceWith,Replace:OleVariant;
begin
FindText:=S;//被替换的字符串
MatchCase:=False;//不区分大小写
MatchWholeWord:=True;//全字匹配
MatchWildcards:=False;
MatchSoundsLike:=False;
MatchAllwordForms:=False;
Forward:=True;
Wrap:=wdFindcontinue;
Format:=False;
ReplaceWith:=D;//替换字符串
Replace:=True;
WordDocument.Range.Find.ExecuteOld(FindText,
matchCase,
MatchWholeWord,
MatchWildcards,
MatchSoundsLike,
MatchAllWordForms,
Forward,
Wrap,
Format,
ReplaceWith,
Replace);
end;
//------------------------------------------------------------------------------
//NameList是你要套打的字段,ValueList是套打字段对应的值
procedure GenerateWordReport(NameList,ValueList:TStringList;DocTemplate: OleVariant);
var NewTemplate,ItemIndex:Olevariant;
i:integer;
begin
try
WordApplication.Connect;
except
MessageDlg('你的电脑没有安装Word!无法生成Word报表!',mtError,[mbOk],0);
Abort;
end;
WordApplication.Visible:=true;
NewTemplate:=False;//不将文档作为模板打开
WordApplication.Documents.AddOld(DocTemplate,NewTemplate);
ItemIndex:=1;
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
wordapplication.options.checkspellingasyoutype := false;
wordapplication.options.checkgrammarasyoutype := false;
For i:=0 to NameList.Count-1do
ReplaceString(NameList,ValueList,WordDocument);
WordApplication.Disconnect;//此句比较关键,可避免产生RPC调用失败的错误
end;
2.定义Word模版xx.doc
3.调用报表生成过程产生具体的报表的例子
var TemplatePath:string;//模版路径
i:integer;
TempNameList,TempValueList:TStringList;
TempYear,TempMonth,TempDay:word;
begin
TemplatePath:=ExtractFilePath(Application.exeName);
TempNameList:=TStringList.Create;
TempValueList:=TStringList.Create;
with XXQuerydo
For i:=0 to FieldCount-1do
begin
if (Fields.FieldName='发货日期')//发货日期需要特殊处理
then
begin
DecodeDate(Fields.AsDateTime,TempYear,TempMonth,TempDay);
TempNamelist.add('年');TempValueList.Add(IntTostr(TempYear));
TempNamelist.add('月');TempValueList.Add(IntTostr(TempMonth));
TempNamelist.add('日');TempValueList.Add(IntTostr(TempDay));
continue;
end;
TempNameList.Add(Fields.FieldName);

TempValueList.Add(Fields.AsString);
end;
GenerateWordReport(TempNameList,TempValueList,TemplatePath+'Template/XX.doc');
TempnameList.Free;
TempValueList.Free;
end;

希望对大家有点帮助...

 
非常好,我收藏
 
没有太看懂实现的思路,能说一下实现思路吗?看你用到了替换,但是没有继续看下来。
我当初实现的思路:
查找需要套打的字段,将他们的字体颜色设置成白色,如果需要套打的是网格,则设置为无网格线方式,在打印完成后再Undo回来。
http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517
 
思路也比较简单:
1.定义好模版文件,比如xx.doc
其中包含了你要进行替换的字段,比如:
姓名,年,月,日
2.然后进行替换
如果数据库中存储的是整个日期,比如2002-10-9,那么你就要将这个日期分解成2002,10,9
来分别替换掉年月日.
3.我主要定义了那个生成Word报表的过程,其中
NameList参数存储的内容包含模版文件中要替换的字段,比如:
姓名,年,月,日
ValueList参数则是对应的值,比如
张三,2002,10,9

 
你的套打的含义?
原来似如何使用Word来打印数据,呵呵。并不是真正意义上的套打,真正的套打是一张纸已经存在了表格或者红头等。
你使用程序去填写表格里面的内容。呵呵。
 
替换是个办法
还有个办法就是用Bookmark对象,很好用的。
 
接受答案了.
 
顶部