在Delphi中很精确地控制生成的WORD文档的格式

来源:互联网 发布:2016网络广告公司排名 编辑:程序博客网 时间:2024/05/01 03:49

原文地址:http://blog.163.com/laidedou@126/blog/static/1150740632012036110729/


var    WordApplication1: TWordApplication;

通过以下的方式就可以在DELPHI中很精确地控制生成的WORD文档的格式。

    //-----------------打开WORD-------------------------------
     Wordapplication1.visible := true;    //显示WROD
    Worddocument1.activate;

    //-----------------设置字体--------------------------------
    wordapplication1.Selection.Font.Size := 14;                //字号
    wordapplication1.Selection.Font.Name := '宋体';          //字体
    wordapplication1.Selection.Font.Bold := wdToggle;      //加粗

    //-------------将图形文件插入文档当前位置--------------------
    Wordapplication1.Selection.InlineShapes.AddPicture('pict.jpg', emptyparam, emptyparam, emptyparam);
   //---------------换行---------------------
    wordapplication1.Selection.TypeParagraph;

    //------------写入文字---------------
    wordapplication1.Selection.TypeText('这是用TypeText方法向文档写入的文字');

    //------------写带下划线的文字----------------
    wordapplication1.Selection.Font.UnderlineColor := wdcolorautomatic; //下划线颜色
    wordapplication1.Selection.Font.Underline := wdUnderlineSingle;        //下划线线型
    wordapplication1.Selection.TypeText('这是下划线文字');
    wordapplication1.Selection.Font.Underline := wdUnderlineNone;         //无下划线

    //---------------文字居中----------------
    wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;   //居中对齐
    wordapplication1.Selection.TypeText('水平对齐文字');
    wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;       //靠左对齐

    //--------------插入分节符,其中 t: olevariant;---------------------
    t := wdPageBreak;
    wordapplication1.Selection.InsertBreak(t);

    //-----------------插入分页符,其中 t: olevariant;----------------------------
    t := wdSectionBreakNextPage;
    wordapplication1.Selection.InsertBreak(t);

    //-------------------生成页眉页脚并写入数据----------
    worddocument1.ActiveWindow.ActivePane.View.SeekView := wdseekcurrentpageheader; //选页眉对象
    wordapplication1.Selection.TypeText('这是页眉文字);
    wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;      //对齐方式
    wordapplication1.Selection.PageSetup.DifferentFirstPageHeaderFooter := -1;             //首页不同
    worddocument1.ActiveWindow.ActivePane.View.SeekView := wdseekcurrentpageFooter;   //选页脚对象
    //页脚上面横线
    wordapplication1.Selection.WholeStory;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderRight).LineStyle := wdLineStyleNone;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle := wdLineStyleSingle;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineWidth := wdLineWidth050pt;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).Color := wdColorAutomatic;
    wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderBottom).LineStyle := wdLineStyleNone;
    wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromTop := 1;
    wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromLeft := 4;
    wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromBottom := 1;
    wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromRight := 4;
    wordapplication1.Selection.ParagraphFormat.Borders.Shadow := False;

    //页脚文字
    wordapplication1.Selection.TypeText('这是页脚文字');
    wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;    //对齐方式
    wordapplication1.Selection.PageSetup.DifferentFirstPageHeaderFooter := -1;

    //切换到文档区域
    wordapplication1.ActiveWindow.ActivePane.View.SeekView := wdseekmaindocument;

    //------------------写带上、下标的文字-------------------------
   Wordapplication1.Selection.TypeText('正常文字1');
   Wordapplication1.Selection.Font.Superscript := -1;    //设置为上标
   Wordapplication1.Selection.TypeText('上标文字');
   Wordapplication1.Selection.Font.Superscript := 0;    //恢复正常文字

   Wordapplication1.Selection.TypeText('正常文字2');
   Wordapplication1.Selection.Font.Subscript:= -1;    //设置为下标
   Wordapplication1.Selection.TypeText('下标文字');
   Wordapplication1.Selection.Font.Subscript:= 0;    //恢复正常文字

   //--------------创建各级标题--------------
   prop := '标题 1';        //var prop: olevariant;
   with Wordapplication1.Selection do
   begin
      Set_Style(prop);          //设为一级标题
      Font.Name := '宋体';    //标题字体
      Font.Size := 14;          //标题字号
      ParagraphFormat.Alignment := wdAlignParagraphCenter;    //标题对齐方式
      ParagraphFormat.SpaceBefore := 12;                  //标题段前距离
      ParagraphFormat.SpaceAfter := 6;                      //标题段后距离
      Font.Bold :=1;                                                   //加粗

      TypeText('标题文字');
      TypeParagraph;

      prop := '正文';                       //恢复正文格式
      Set_Style(prop);
      Font.Name := '宋体';
      Font.Size := 10.5;
      Font.Bold :=0;
   end;

   //--------------------表格处理---------------------------
   //其中:t: olevariant; TableName: string; row, col: integer;

     with Wordapplication1.Selection do  

            //写表名
            ParagraphFormat.Alignment := wdAlignParagraphCenter;   
            ParagraphFormat.SpaceBefore := 6;
            ParagraphFormat.SpaceAfter := 6;
            TypeText('表1 ' + TableName);
            TypeParagraph;

            //插入表格
             t := doc.tables.Add(Range, row, col, 1, 0);    //插入row行col列的表
            t.range.ParagraphFormat.Alignment := wdAlignParagraphCenter;    //表格居中
            t.Range.Font.Size:=9;       //单元格字号为小五
             t.range.Cells.VerticalAlignment := wdCellAlignVerticalCenter;     //单元格垂直居中
            t.range.Rows.Alignment := wdAlignRowCenter;                          //单元格水平居中
            //设置每列宽度
            t.Columns.Item(1).Width:=40;
            t.Columns.Item(2).Width:=92;
            ……

           //合并单元格
            t.cell(9, 4).merge(t.cell(9, 5));

        //向单元格写入文字
          t.cell(1, 1).range.text := '序号';   //第一行第一列
        t.cell(1, 2).range.text := '姓名';   //第一行第二列
     end;

    //----------页面设置:修改纸张为A3横向------------
    with wordapplication1.Selection.Range.PageSetup do
    begin
          Orientation := wdOrientLandscape;
          TopMargin := 3.17 * 28.35; 
          BottomMargin := 3.17 * 28.35; 
          LeftMargin := 2.54 * 28.35; 
          RightMargin := 2.54 * 28.35;
          Gutter := 0; 
          HeaderDistance := 1.5 * 28.35; 
          FooterDistance := 1.75 * 28.35;
          PageWidth := 42 * 28.35; 
          PageHeight := 29.7 * 28.35;

          FirstPageTray := wdPrinterDefaultBin;
          OtherPagesTray := wdPrinterDefaultBin;
          SectionStart := wdSectionNewPage;
          DifferentFirstPageHeaderFooter := 0;
          VerticalAlignment := wdAlignVerticalTop;
          SuppressEndnotes := 0;
          MirrorMargins := 0;
          TwoPagesOnOne := False;
          BookFoldPrinting := False;
          BookFoldRevPrinting := False;
          BookFoldPrintingSheets := 1;
          GutterPos := wdGutterPosLeft;
          LayoutMode := wdLayoutModeLineGrid;olevariant;
    end;

   //------------自动插入目录--------------------------

   //各级目录字型控制
    t := '目录 1';
    Wordapplication1.ActiveDocument.Styles.Item(t).Font.Bold:=1;
    t := '目录 2';
    Wordapplication1.ActiveDocument.Styles.Item(t).Font.Bold:=1;

    //这里是生成目录
    UseHeadingStyles := True;       //以下变量均为olevariant类型
    UpperHeadingLevel := 1;
    LowerHeadingLevel := 4;
    usefields := 1;
    talbeid := 1;
    RightAlignPageNumbers := True;
    IncludePageNumbers := True;
    Wordapplication1.ActiveDocument.TablesOfContents.add(Wordapplication1.Selection.Range,
         UseHeadingStyles, UpperHeadingLevel, LowerHeadingLevel, usefields, talbeid, RightAlignPageNumbers,
          IncludePageNumbers, IncludePageNumbers, IncludePageNumbers, IncludePageNumbers,
         IncludePageNumbers);

    //保存文档
    t := false;
    WordDocument1.SaveAs(FileName, t);   //存在Filename时,不提示直接保存

    //关闭WORD
    Wordapplication1.Disconnect;

0 0
原创粉丝点击