`

delphi操作word -- 转

阅读更多


转自 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.

  • 大小: 26.3 KB
  • 大小: 24.6 KB
  • 大小: 9.3 KB
分享到:
评论

相关推荐

    delphi中操作word

    在本项目中,我们探讨的是如何使用Delphi来操作Microsoft Word,特别是涉及到在Word文档中插入表格以及对表格的行、列进行操作。 首先,让我们深入了解一下如何在Delphi中与Word交互。这通常通过使用Microsoft ...

    delphi操作word实例

    Delphi 操作 Word 实例详解 在 Delphi 中操作 Word 文档是一种常见的需求,特别是在财务、文档管理等领域。在这个示例中,我们将学习如何使用 Delphi 操作 Word 文档,实现自动化生成 Word 文档的功能。 Delphi ...

    Delphi操作Word演示

    "Delphi操作Word演示"是一个示例项目,它展示了如何利用Delphi编程环境与Microsoft Word交互,以便进行文档创建、编辑或自动化处理。Delphi是一款强大的Windows应用开发工具,使用Object Pascal语言,而Word则是常用...

    delphi7操作word

    以上就是使用Delphi 7操作Word的基本步骤和主要知识点。在实际开发中,你可能还需要处理更多细节,比如错误处理、用户交互、更复杂的表格格式设置等。同时,注意Word自动化可能会因为用户没有安装相同版本的Word或...

    delphi操作word插入文字图片.rar

    本示例中,"delphi操作word插入文字图片.rar"是一个压缩包,包含了使用Delphi 10.2实现向Word文档插入文字和图片的代码或项目。这个功能对于自动化文档处理和报告生成尤其有用。 首先,我们需要理解如何在Delphi中...

    delphi_word打印

    在Delphi编程环境中,开发人员经常需要处理与外部应用程序的集成,例如Microsoft Word。本教程将深入探讨如何在Delphi中使用OleContainer组件来打开Word文档并实现打印功能。这个过程涉及到几个关键知识点,包括Ole...

    用Delphi将Word文档转换为TXT文件

    使用Delphi操作Word文档 #### 1.1 创建Word应用程序对象 ```delphi wordapp := CreateOleObject('Word.Application'); ``` 通过`CreateOleObject`函数创建一个指向Word应用程序的对象实例。此对象允许我们在Delphi...

    Delphi操作word文档.rar_Delphi操作word文档_delphi word_delphi 文档_word

    1. **使用ActiveX控件**:Delphi提供了TWordDocument组件,它是通过Ole Automation来操作Word的。首先,需要在项目中导入`ComObj`单元,这个单元包含了对ActiveX控件的支持。然后在界面上添加TWordDocument组件,并...

    delphi操作word基类

    用delphi编程操作WORD的一个基类,实现了插入表格,图片,文字等常用操作,功能很全面,是学习DELPHI操作WORD很好的例子

    delphi操作word excel

    综上所述,Delphi 操作 Word 和 Excel 主要是通过 OLE 自动化技术实现,涉及到组件的使用、事件处理、数据库数据与 Excel 数据交换等多个环节,这些都需要开发者具备一定的 Delphi 和 OLE 自动化知识。

    Delphi实现Word、PDF文档转Image

    在实现"Word转Jpg"的过程中,通常会利用Windows API或第三方库来读取Word文档内容,并将其渲染为图像。对于PDF文档,可能需要使用专门的PDF解析库,如Ghostscript或iText,来获取页面内容并转换成图像。 在这个项目...

    delphi读取word文档

    在压缩包内的“读取word文档”源码程序中,你可能会看到类似以上代码的实现,通过逐行阅读和理解代码,你可以更深入地了解如何在Delphi中操作Word文档。注意,使用Word Automation可能会消耗较多系统资源,因此在...

    DELPHI 2010 调用word

    除了直接使用OLE自动化接口,还有一些第三方库,如TMS Software的TMS Office Pack,提供了更方便、更高级的接口来操作Word,降低了直接使用OLE的复杂性。 总的来说,DELPHI 2010调用Word主要依赖于COM组件和OLE...

    Delphi7.0 操作word填写表格

    在标题和描述中提到的关键知识点包括:Delphi 7.0、操作Word、填写表格、代码示例。 1. Delphi 7.0是一种集成开发环境(IDE),用于快速开发跨平台、多层结构、数据库驱动的应用程序。它是使用Object Pascal编程语言...

    delphi窗体嵌入word

    在Delphi编程环境中,开发人员有时需要将应用程序的窗体(Form)与Microsoft Word文档集成,以便实现诸如编辑、查看或者交互式操作Word文档的功能。本文将深入探讨如何在Delphi XE2版本中实现窗体嵌入Word的详细步骤...

    DocReplace_打开word_delphi_delphi替换Word_delphi合同_

    在IT行业中,尤其是在文档处理和自动化办公领域,利用编程语言如Delphi进行Word文档操作是一项常见且实用的技术。本文将详细解析"DocReplace_打开word_delphi_delphi替换Word_delphi合同_"这一主题,以及如何利用...

    delphi中提取word内容和图片

    在Delphi编程环境中,我们可以利用COM对象(Component Object Model)技术来操作Microsoft Office应用程序,如Word,从而实现从Word文档中提取内容和图片的功能。在本文中,我们将深入探讨如何在Delphi XE环境下,...

    delphi对Word操作

    在delphi中使用createoleobject方法对word文件进行操作 使用CreateOleObject方法对WORD文档操作具有先天所具备的优势,与delphi所提供的那些控件方式的访问相比,CreateOleObject方法距离WORD核心的操作“更近”,...

    Delphi版Word文档批量合并、文字替换、修改、插入图片程序.rar

    Delphi开发的Word文档批量处理程序,可批量Word文字替换,批量字体修改、批量插入图片,批量合并Word文档、删除指定页等功能,还可以批量新建文档。关于本程序的使用说明:  1、系统将根据样版文件复制成多个文件;...

    Delphi操作Word源码

    Delphi7操作Word源码\ Delphi7操作Word源码\ Delphi7操作Word源码\

Global site tag (gtag.js) - Google Analytics