======================================================
注:本文源代码点此下载
======================================================
blueski推荐 [2006-11-1]
出处:瀚海星云 bbs.ustc.edu.cn
作者:jamesread张俊
在科大的bbs上,我一直回答问题多,少专门发表个人观点,不如这次就来一篇。
以下纯粹是我的个人观点,欢迎大家批评指正.
这段时间bbs上有人又讨论到报表问题,我觉得必须重视这个问题,因为在实际的软
件开发中,打印出来报表十分关键,一个公司不仅需要电子文档,还经常需要看的见还摸得着的实在的纸的文件,比如通知、档案、帐目单、汇总表等。恰好前段时间有人问我作报表用什么最好,所以我写一文如下供大家参考。
当时我给他推荐过fastreport但后来又针对他所要做的大报表(报告书之类)推荐他
使用word.他不解,我说:有的人说delphi5~6的quickreport简单,有的人说delphi7上的rave不错,有的人说fastreport控件功能强大。但我个人认为quickreport问题多多(bbs的shujian 在7926中不是说报表有问题吗?),delphi7后的版本已经淘汰。rave为borland推荐,但是属于外国人新开发的控件,中文支持未必好。fastreport为许多人所推荐(xiyu也推荐,我有时也推荐),但要下载和注册控件,而且帮助常常不全生成报表后用户也不好再修改了.
在我和客户打交道的过程中,我觉得如果操作员不是计算机专业出身的,而且生成的
报表比较长或改动比较多和对生成报表的时间要求不高的时候,比如是结算表,项目书之类的操作员和管理者都认为不如把报表导出到他们所熟悉的excel和word中去.
(以前我吃过亏,做结算表的时候用quickreport,无法修改而且统计功能跟不上公司的变
化,一年后不得不应要求修改软件增加了报表导出到excel的功能,这样可以让他们自己到excel进行一些我至今无法了解的统计操作)
1.导出到excel我在bbs前面的讨论中推荐过,现在复述一遍:
用f1book6(注我试验时候发现delphi6的f1book的保存或打开不管用,所以推荐下载f
1book6控件) 和或excelapplication,前者比较快,网上有很多例子。
2 导出到word (网上的例子和讨论较少,ming_xiang在bbs上问过,没人答,所以我附一
例 )
优点为
(1)可以有很好的界面。
(2)生成的报表可以做一些必要的修正。
(3)弹出的错误消息是word内部的中文提示,用户感到亲切。
(4)用户比较熟悉word的各种操作(比如说修改字体和颜色)。
(5)用户可以使用word的统计功能或拷贝到其他有强大统计功能的软件如excel中。
(6)对各种机器的兼容性好,不需要注册控件。
(7) 没想全...大家可以补充。
缺点
(1) 老机器上速度慢
(2) 多不许修改的报表不适合
(3) 还没想到...大家可以补充。
我最近就用wordapplication 做了一个word报表,调试成功了,简单复制格式如下。我
加了不少的中文说明,大家可以参考或批评指正
当然大家也可以参考bbs的5532的unitword单元代码
例子:
在server中加入 wordapplication(word服务器控件),worddocument(word 文档)
和wordfont(设word字体的控件) 和一个 dbimage(将用它放图片到word中)
procedure tformcx1.btnwordclick(sender: tobject);
//新建word并填入信息
var i,j:integer;template,newtemplate,wddocumenttype,wdvisible
,itemindex:olevariant;
filename, confirmconversions, count,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format
:olevariant;
procedure setfont(size,bold,underline:integer);
//设字体的过程
begin
wordfont.connectto(worddocument.sentences.get_last.font);
wordfont.size := size;//24;
wordfont.bold := bold;//1;
wordfont.underline := underline;//2
//也可以设置字体的其它属性:
//wordfont.italic := 0;//1;
// wordfont.emboss := 1;
// wordfont.engrave :=0;// 1;
//wordfont.shadow :=0;// 1;
//wordfont.doublestrikethrough := 1;
// wordfont.strikethrough := 0;//1;
//wordfont.name :='宋体';
end;
begin
try
template := emptyparam;//设定新建的word所使用的模板
newtemplate := true;
itemindex := 1;//设定文档号
try
wordapplication.connect;//呼叫word服务
except
messagedlg('也许word没有安装,请重试', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;//设新建的word文档为可见
wordapplication.caption := 'delphi automation';//设新建的word文档的名称
{create new document}
template := emptyparam;
newtemplate := false;
wddocumenttype := $00000000; // $00000001;
wdvisible := true;
//创建新的word文档,如下
wordapplication.documents.add(template, newtemplate);
//*************************
//注:如果要打开已经有的word文件,则如下代码,
{open document}
//有时候 我喜欢用(**)表明为注释而不是{}因为{}有时用作编译开关和窗体声明
,有时有歧义
(*格式:worddocuments.open(filename,confirmconversions,readonly,passworddoc
ument,
passwordtemplate,revent,writepassworddocument,writepasswordtemplate,
format,encoding,visible)
filename:文档名(包含路径)
confirmconversions:是否显示文件转换对话框
readonly:是否以只读方式打开文档
addtorecentfiles:是否将文件添加到"文件"菜单底部的最近使用文件列表
中
passworddocument:打开此文档时所需要的密码
passwordtemplate:打开此模板时所需要的密码
revert:如果文档已经,是否重新打开文档
writepassworddocument: 保存对文档更改时所需要的密码
writepasswordtemplate: 保存对模板进行更改时所需要的密码
format:打开文档时所需使用的文件转换器
encoding:所使用的文档代码页
visible:打开文档的窗口是否可见 *)
(*例:
filename := extractfiledir(application.exename)+'/中科院项目书.doc';
confirmconversions := false;
readonly := false;
addtorecentfiles := false;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument;
wordapplication.documents.open( filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format );
*)
//***************************
{assign worddocument component}
//连接word里新建的第一个document文档
worddocument.connectto(wordapplication.documents.item(itemindex));
//设定文档模式
{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}
//********************
//插入图片
dbimage1.copytoclipboard;//把dbimage1控件里的图拷贝到粘贴板
worddocument.sentences.last.paste;//把粘贴板的内容贴到word文末
//插入文字
withworddocument.range do begin
setfont(20,1,0);//设字体为有下划线的20号的黑体字
insertafter('项目编号:' );
insertafter('00000001'+#13);
insertafter(' ' + #13);//插入换行符号
insertafter(' ' + #13);
setfont(30,1,1);
insertafter('中科院项目书' + #13);
setfont(20,1,0);
insertafter(' 类别:重点计划' );
insertafter('项目名称' );
insertafter(' ' + #13#10);
//略一部分
//插入表格
//插入5行5列的表一
worddocument.tables.add(worddocument.sentences.last ,5,5 ) ;
with worddocument.tables.item(1) do begin
cell(1,1).range.text:='项目名称';
cell(1,2).range.text:='单位情况';
cell(1,3).range.text:='申请人';
cell(1,4).range.text:='电话';
cell(1,5).range.text:='签字';
end;//with worddocument.tables.item(1)
insertafter(' ' + #13);
setfont(20,1,0);
insertafter(' ' + #13#10);
//插入表2
worddocument.tables.add(worddocument.sentences.last,1,1) ;
insertafter(' ' + #13#10);
with worddocument.tables.item(2) do begin
cell(1,1).range.text:='我要省略啦';//...略
end;// with worddocument.tables.item(2)
end;//withworddocument.range
//通知服务结束,可以中断连接
wordapplication.disconnect;
except//如果word的服务有问题则跳出并弹出异常消息
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;//try
end;
procedure tform1.closewordclick(sender: tobject);
//关闭word
var
savechanges,
originalformat,
routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end;
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/