原文地址: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;

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

  1. npoi html导出word,NPOI插件生成导出word文档

    由于以前没有接触NPOI过这个插件,因此几乎都是本身一边百度摸索一边学习.html 这个插件对于Excel的数据导入和导出,能够说是很方便了,前端 可是对于导出word文档,能够说是不多的,百度了不少 ...

  2. 快速将非Word中的不可编辑的公式转换为Word文档中公式编辑器里可编辑的公式的办法

      因为我最近需要翻译一篇外文文献,其中的公式比较多,如果我自己去Word里用公式编辑器编辑,那么会翻译的会很慢,于是就搞出了一套可以自动将非Word中的不可编辑的公式转换为Word中可编辑的公式的办 ...

  3. Android开发——使用Jword生成本地word文档

    本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下. 为什么使用Jword呢?因为IText .Freemark在安卓平台上压 ...

  4. ftl模板生成的word文档打不开

    使用ftl用ftl模板生成的word文档打不开,一直未响应的可能问题如下: 1.word模板另存为xml格式文件的时候,选择Word 2003 XML格式(兼容问题) 2.模板中有图片的时候,将ftl ...

  5. 永磁同步直线电机仿真实例,仿真教学 maxwell16.0版本 包括图中模型以及一个仿真设置要点word文档教程

    永磁同步直线电机仿真实例,仿真教学 maxwell16.0版本 12槽11极 包括图中模型以及一个仿真设置要点word文档教程 ID:69100643350855880陈cola

  6. OpenXml编程--去除自动生成的word文档中由分页符和换行符产生的空白页

    前言 前置知识:OpenXml 首先描述下问题产生的场景.我们的业务需求是根据用户的在线作答(或导入的作答结果)数据批量产生报告.产生报告的方式是把通过工作流控制的复杂业务逻辑的产出--分析结果--和 ...

  7. springboot下生成复杂word文档方案 在Word软件里面制作模板

    系列文章目录 第二章:根据模板导出word,复合格式表格生成.可变列表格生成 第三章:doc和docx插入多图 第四章:web环境下word文档预览 springboot freemarker 生成复 ...

  8. html中可以打钩的小方块,Word文档怎么实现在小方块里打钩? | 我爱分享网

    今天小编和大家一起学习的是Word 2010文档怎么实现「在小方块里打钩」?一定有许多朋友很是期待吧,下面就是详细的内容(以Word 2010为例). 操作步骤: 1.在你的电脑上打开一个Word文档 ...

  9. excel中设置下拉选择框和word文档中插入外部excel只显示图标

    这篇文章介绍两个和excel相关的主题,一个是对excel表格设置下拉框,另一个是在word文档中插入外部excel,但是我们不想展示excel的内容,而是展示一个图标,点击图标,可以打开系统中的文件 ...

最新文章

  1. WMI技术介绍和应用——Event Consumer Provider
  2. 什么是微分?什么是导数?如何利用微分-导数方程求导数?
  3. 在三角形中rt是什么意思_数学中RT三角形是什么意思
  4. 小记:在WSS语言包升级问题
  5. Android 使用正则表达式验证邮箱格式是否正确
  6. java电商面试介绍
  7. 从拉格朗日乘数法到KKT条件
  8. Python学习笔记:安装OBSFTP时出现的版本异常问题
  9. PHP手机壳DIY定制平台源码 Thinkphp内核开发
  10. 使用 Python 实现多进程
  11. python call agilent com_python 控制Asterisk AMI接口外呼电话的例子
  12. 读大道至简第五章有感
  13. 【转】世界顶级人工智能会议的总结
  14. ECSHOP解决大量重复页面问题,提高SEO效果
  15. Linux系统中 安装Vmware Toolst工具
  16. java 典型程序,13道Java典型的Java小程序
  17. IDM磁力解析 IDM磁力使用教程
  18. 龙芯2h芯片不能进入pmon_基于龙芯2F架构的PMON分析与优化
  19. VBA读excel写xml
  20. 百度php获取当前经纬度,百度地图获取经纬度的示例

热门文章

  1. 实现企业邮箱登录验证功能
  2. 2020年11月 工信部考试——Hadoop(数据应用技术)中级认证3
  3. 西门子S7-1200控制V90伺服
  4. 计算机电缆芯数,DJYVP22电缆|电线(直径、重量、芯数)
  5. 教你用Python绘制炫酷专业插图
  6. 辽宁大学计算机专业基础考试题型,2018年辽宁大学854计算机专业基础考研大纲...
  7. 教你如何用手机打特殊符号、日文、韩文、繁体字。::======
  8. 不逼自己下不知道潜力可以有多大
  9. D2D与其他短距离通信技术
  10. 20071020--高桥