- 浏览: 174076 次
- 来自: ...
文章分类
最新评论
-
fsword:
同事拿着试了一下,说是把第五步放到第二步之前就可以了
在windows下安装ruby1.9.2,rails3.0.3和DevKit--转 -
dothwinds:
[flash=200,200][/flash][url][/u ...
打包ruby or rails成exe(英文)
转自 http://xxzqb.blog.163.com/blog/static/4122142920089249514506/
开发原因:公司财务要每月与专卖店对帐,其对帐单格式是统一的,只是每月改变其中的数据。
如下图:
所以,我设置了一个模板,名字为 xldzd.dot ,格式如下:
即把每月要修改的数据做成批注。
然后在 d:\对帐单 目录下创建 每月的目录,其名称为日期,如: d:\对帐单\2008-10 。(这部分是由程序产生)
然后,查询数据表(adoquery),
在刚才建立的目录下,产生word文档,上面查询出的数据依次替换 批注,完毕后,删除批注,并且关闭该word文档,然后继续替换,每一个专卖店产生一个word文档,其名称为专卖店的email 。 在电脑中的结果如下图:
程序的关键语句在 procedure TForm9.Button3Click(Sender: TObject);
unit Unit9;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls, DBXpress, SqlExpr, DBTables,
WordXP, OleServer, ComCtrls;
type
TForm9 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
WordDocument1: TWordDocument;
WordApplication1: TWordApplication;
OpenDialog1: TOpenDialog;
Button3: TButton;
StatusBar1: TStatusBar;
ADOConnection2: TADOConnection;
ADOQuery2: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure deletecomment();
procedure writecomment(explans:array of string);
procedure wordini(template:olevariant);
procedure setopendialog;
procedure substpostils(postils:array of string);
procedure deletepostils;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form9: TForm9;
ptotal:integer;
implementation
uses Unit10;
{$R *.dfm}
procedure TForm9.deletecomment;
var
total:integer;
begin
total:=worddocument1.Comments.Count;
while total<>0 do
begin
worddocument1.comments.item(total).Delete;
total:=worddocument1.comments.Count;
end;
end;
procedure TForm9.wordini(template: olevariant);
var
newtemplate,itemindex:olevariant;
begin
newtemplate:=false;
itemindex:=1;
try
wordapplication1.Connect;
except
wordapplication1.Disconnect;
messagedlg('请安装Office中的Word软件!',mterror,[mbok],0);
exit;
end;
wordapplication1.Visible:=true;
wordapplication1.Documents.Add(template,newtemplate,emptyparam,emptyparam);
worddocument1.ConnectTo(wordapplication1.Documents.Item(itemindex));
ptotal:=worddocument1.Comments.count;
end;
procedure TForm9.writecomment(explans: array of string);
var
i,total:integer;
begin
total:=worddocument1.Comments.Count;
for i:=0 to total do
worddocument1.Comments.item(i).Scope.Text:=explans[i-1]
end;
procedure TForm9.setopendialog;
var
pathname,filename:string;
begin
//保证选择的文件必须存在
opendialog1.Options:=[offilemustexist];
opendialog1.filter:='word files(*.dot)|*.dot|all files(*.*)|*.*';
//设置*.dot类型为缺省类型
opendialog1.FilterIndex:=1;
//设置文档模板文件名为缺省值
opendialog1.FileName:='xldzd.dot';
if not opendialog1.Execute then
exit;
pathname:=extractfilepath(opendialog1.FileName);
filename:=extractfilename(opendialog1.FileName);
if lowercase(copy(filename,length(filename)-4+1,4))<>'.dot' then
begin
messagedlg('请打开Word模板文件!',mterror,[mbok],0);
exit;
end;
if lowercase(filename)<>'xldzd.dot' then
begin
messagedlg('请选择正确的Word模板文件!',mterror,[mbok],0);
exit;
end;
end;
procedure TForm9.substpostils(postils: array of string);
var
i:integer;
begin
//逐个替换批注
for i:=1 to ptotal do
begin
worddocument1.Comments.Item(i).Scope.Text:=postils[i-1];
end;
end;
procedure TForm9.deletepostils;
var
total:integer;
begin
total:=ptotal;
while total<>0 do
begin
//删除文档中的一个批注
worddocument1.comments.Item(total).Delete;
//获得文档中的剩余批注总数
total:=worddocument1.Comments.Count;
end;
end;
procedure TForm9.Button1Click(Sender: TObject);
begin
//查询富友中的数据(太慢)
with adoquery2 do
begin
close;
sql.clear;
sql.add('select kcckdm,sum(kcqcje),sum(kcrkje),sum(kcckje) from kct06');
sql.add('where kckjnd=2008 and kckjyf=8');
sql.Add('group by kcckdm');
sql.add('order by kcckdm');
open;
//下面代码没问题
{close;
sql.clear;
sql.add('select * from ylm03');
open; }
end;
end;
procedure TForm9.Button3Click(Sender: TObject);
var
postils:array of string;
//y,m,d:word;
pathname,filename:string;
savedocfile,emptypara:olevariant;
//这些都是目录名称
dir:string;
currdir:string;
dirname:string;
curr_dir:string;
begin
//先建立文件夹 (放在d:\对帐单\下)
//先判断文件夹是否已存在 , 不存在,就创建之
if not directoryexists('d:\对帐单') then
begin
try
//创建文件夹
mkdir('d:\对帐单');
statusbar1.Panels[2].Text:=' ① d:\对帐单 目录创建成功!';
except
statusbar1.Panels[2].Text:=' ③ d:\对帐单 目录无法创建!';;
exit;
end;
end else
begin
statusbar1.Panels[2].Text:=' ② d:\对帐单 目录已经存在!';
end;
//把日期当前日期当作新建文件夹的名称
dir:=formatdatetime('yyyy-mm',date);
try
//改变当前文件夹为 d:\对帐单
chdir('d:\对帐单');
//取得当前文件夹,赋值给变量 currdir
getdir(0,currdir);
statusbar1.Panels[2].Text:=' ④ 当前目录为:'+currdir;
except
end;
//在文件夹 d:\对帐单 下根据日期创建文件夹,文件夹名为日期
if not directoryexists('d:\对帐单\'+dir) then
begin
try
//创建文件夹
mkdir('d:\对帐单\'+dir);
statusbar1.Panels[2].Text:=' ⑤ d:\对帐单\'+dir+' 目录创建成功!';
except
statusbar1.Panels[2].Text:=' ⑥ d:\对帐单\'+dir+' 目录无法创建!';;
exit;
end;
end else
begin
statusbar1.Panels[2].Text:=' ⑦ d:\对帐单\'+dir+' 目录已经存在!';
//exit;
end;
//转换当前文件夹至 d:\对帐单\日期
try
//改变当前文件夹为 d:\对帐单\日期
chdir('d:\对帐单\'+dir);
//取得当前文件夹,赋值给变量 curr_dir
getdir(0,curr_dir);
statusbar1.Panels[2].Text:=' ⑧ 当前目录为:'+curr_dir;
except
end;
//------------------------------------------------------------
//setopendialog;
//保证选择的文件必须存在
opendialog1.Options:=[offilemustexist];
opendialog1.filter:='Word Files(*.dot)|*.dot|All Files(*.*)|*.*';
//设置*.dot类型为缺省类型
opendialog1.FilterIndex:=1;
//设置文档模板文件名为缺省值
opendialog1.FileName:='xldzd.dot';
//如果打开对话框没有运行,就退出
if not opendialog1.Execute then
exit;
//赋文件名
pathname:=extractfilepath(opendialog1.FileName);
filename:=extractfilename(opendialog1.FileName);
if lowercase(copy(filename,length(filename)-4+1,4))<>'.dot' then
begin
messagedlg('请打开Word模板文件!',mterror,[mbok],0);
exit;
end;
if lowercase(filename)<>'xldzd.dot' then
begin
messagedlg('请选择正确的Word模板文件!',mterror,[mbok],0);
exit;
end;
//查询出要插入的数据表数据
with adoquery1 do
begin
close;
sql.clear;
sql.add('select * from xldzd');
open;
first;
//赋值给批注
while not adoquery1.Eof do
begin
wordini(pathname+filename);
setlength(postils,ptotal);
postils[0]:=trim(fieldbyname('t1').asstring);
postils[1]:=trim(fieldbyname('t2').asstring);
postils[2]:=trim(fieldbyname('t3').asstring);
postils[3]:=trim(fieldbyname('t4').asstring);
postils[4]:=trim(fieldbyname('a1').asstring);
postils[5]:=trim(fieldbyname('a2').asstring);
postils[6]:=trim(fieldbyname('a3').asstring);
postils[7]:=trim(fieldbyname('a4').asstring);
postils[8]:=trim(fieldbyname('a5').asstring);
postils[9]:=trim(fieldbyname('b1').asstring);
postils[10]:=trim(fieldbyname('b2').asstring);
postils[11]:=trim(fieldbyname('b3').asstring);
postils[12]:=trim(fieldbyname('b4').asstring);
postils[13]:=trim(fieldbyname('b5').asstring);
//填充批注
substpostils(postils);
//删除批注
deletepostils;
//保存目录及文件 ,文件名为 email.doc
savedocfile:=curr_dir+'\'+fieldbyname('email').asstring+'.doc';
emptypara:=emptyparam;
//保存文档
worddocument1.SaveAs(savedocfile,emptypara);
//关闭该文档
worddocument1.Close;
next;
end;
end;
statusbar1.Panels[3].Text:=' 对帐单已全部保存完毕!';
//最后关闭word
wordapplication1.Disconnect;
end;
end.
发表评论
-
将C++ 转为 Pascal/Delphi -- 转
2011-07-23 00:39 2955将C++ 转为 Pascal/Delphi ===== ... -
Delphi编程注意事项--转
2011-07-23 00:35 1278Delphi编程注意事项 任何单元必须有对应测试 ... -
Delphi循环语句对应的汇编语句序列-- 转
2011-07-23 00:33 1227count: 终值, start:起始值正向: ... -
Delphi7快捷键--转
2011-07-21 23:43 1719分类 快捷键 解释 ... -
Delphi写数据至WORD--转
2011-07-21 22:27 33791.所需的三个控件: ChooseWA: ... -
如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中--转
2011-07-21 22:06 3024用image字段保存这些文档。 var ... -
delphi 7实现word文档的分页读取
2011-07-21 22:03 1802问题仔细描述,如果 ... -
Delphi中的数据添加到Word模板的相应位置--转
2011-07-21 22:01 1622就跟将数据导入Excel一样,在运行Delph ... -
delphi控制 word的几种方法--转
2011-07-21 21:49 2339对几种方法的难易程度的判别 a.通过Delphi的控件TOl ... -
Delphi控制Word的报表例子介绍--转
2011-07-21 21:46 2706说明:WordApp是Server下的TWordAppl ... -
delphi 为word文档增加一个段落样式--转
2011-07-21 21:44 1916下为程序中的一段,解决了为文档增加一个段落样式。这个段落样式的 ... -
delphi常见属性事件说明
2010-07-20 21:54 1302DELPHI常见属性说明:align 设置组件对齐(同 ... -
C,Delphi,PHP 基础语法对比[转载]
2010-07-20 20:40 944说明 C(区分大小写) ... -
DELPHI控件安装与删除
2010-07-19 22:49 2400DELPHI控件安装与删除控件安装 基本安装 1、对于单个控件 ... -
『Delphi』File not found的解决办法
2010-07-19 22:48 2657当程序启动,提示Fatal Erro:File not fou ... -
Delphi StringGrid控件的属性及使用说明[转载]
2010-07-19 21:01 5881Delphi StringGrid控件的属 ...
相关推荐
Delphi操作Word的演示. 编译脚本的命令用brcc32命令
delphi 操作 word excel
用delphi编程操作WORD的一个基类,实现了插入表格,图片,文字等常用操作,功能很全面,是学习DELPHI操作WORD很好的例子
Delphi中使用Olecontainer打开Word文档后,并实现打印功能
delphi7操作word,插入文字,表格
Delphi操作word文档,实现word功能
在word中插入表格,对行、列进行操作。本人在开发项目中所写。对于在项目开发中涉及到word表格操作很有用。delphi源码。
delphi用ole方式操作word, 环境delphiXE,支持word2010 Word 97 - 2003
一个DELPHI操作WORD的实例,图文并茂,很有参考价值
一段原创Delphi7.0 操作word进行word文件表格填写的代码,本人业余爱好者,如有错误,敬请诸位大猿指出。
delphi 10.2 实现的 向word 标注了书签的位置插入 文字,或者图片。内码亲测可以。。里面路径自己替换下就ok了。免费供新手参考
控制word类 function SelectLocatPage(PageNum:integer):Range; //选择指定页面,返回选择范围 function GotoLocatPage(i:integer):Range; //移动到指定页面 返回一个范围 function getPageSize:Integer; //取得...
在delphi中使用createoleobject方法对word文件进行操作 使用CreateOleObject方法对WORD文档操作具有先天所具备的优势,与delphi所提供的那些控件方式的访问相比,CreateOleObject方法距离WORD核心的操作“更近”,...
Delphi7操作Word源码\ Delphi7操作Word源码\ Delphi7操作Word源码\
delphi用ole方式操作word, 支持word2010 Word 97 - 2003
Delphi开发的Word文档批量处理程序,可批量Word文字替换,批量字体修改、批量插入图片,批量合并Word文档、删除指定页等功能,还可以批量新建文档。关于本程序的使用说明: 1、系统将根据样版文件复制成多个文件;...
使用Delphi 7控制Word 2000生成文档的方法.
用Delphi开发的,可以根据excel需求跟踪矩阵,按word或excel模板自动转换成需求说明书...通过ole方式来操作word和excel,是作者寻找了众多网上资料包括官方网站的资料才开发出来。学习、参考和直接使用这个工具都可以!
delphi操作word和excel,已经改错编译通过,但是对不同office有问题
摘要:Delphi源码,文件操作,OLE,自动化控制器 Delphi使用Word自动化服务器插入数据到Word程序中,这个程序演示了使用Word作为自动化服务器,Delphi地自动化控制器是如何将一个查询结果插入到word文档中,返回应用...