1、创建Word

public class OpenXmlWordUtility
{/// <summary>/// 文件全路径/// </summary>public string FilePath { get; private set; }/// <summary>/// 处理文档/// </summary>public WordprocessingDocument ProcessingDocument { get; private set; }/// <summary>/// 文档/// </summary>public Document Document{get{return this.ProcessingDocument.MainDocumentPart.Document;}}/// <summary>/// 打开文档/// </summary>public void OpenDocument(){this.ProcessingDocument = WordprocessingDocument.Open(this.FilePath, true);this.TitleParagraphs = this.GetTitleParagraphs();}/// <summary>/// 创建Word文档/// </summary>public void CreateDocument(){this.ProcessingDocument = WordprocessingDocument.Create(this.FilePath, WordprocessingDocumentType.Document);MainDocumentPart mainDocumentPart = this.ProcessingDocument.AddMainDocumentPart();mainDocumentPart.Document = new Document(new Body());Body body = mainDocumentPart.Document.Body;SectionProperties sectionProperties = new SectionProperties();PageSize pageSize = new PageSize();PageMargin pageMargin = new PageMargin();DocumentFormat.OpenXml.Wordprocessing.Columns columns = new DocumentFormat.OpenXml.Wordprocessing.Columns() { Space = "220" }; //720  DocGrid docGrid = new DocGrid() { LinePitch = 100 }; //360  sectionProperties.Append(pageSize, pageMargin, columns, docGrid);body.Append(sectionProperties);mainDocumentPart.Document.Save();}/// <summary>/// 保存文档/// </summary>public void SaveDocument(){this.ProcessingDocument.MainDocumentPart.Document.Save();}/// <summary>/// 关闭文档/// </summary>public void CloseDocument(){if (this.ProcessingDocument != null){this.ProcessingDocument.Close();this.ProcessingDocument.Dispose();}}/// <summary>/// 清除指定标题下的所有内容/// </summary>public void ClearTitleContent(WordTitle title){var index = this.WordTitles.IndexOf(title);//title.Paragraph.NextSibling().RemoveAllChildren();//return;OpenXmlElement startElement = title.Paragraph.NextSibling();OpenXmlElement stopElement = null;if (index != this.WordTitles.Count - 1){var tempLt = this.WordTitles.Skip(index + 1);var tagTitle = tempLt.FirstOrDefault(p => p.Level == title.Level);if (tagTitle != null){stopElement = tagTitle.Paragraph;OpenXmlElement tempElement = startElement;List<OpenXmlElement> openxmleList = new List<OpenXmlElement>();while (tempElement != stopElement){openxmleList.Add(tempElement);tempElement = tempElement.NextSibling();}foreach (var openXmlElement in openxmleList){openXmlElement.Remove();}}else // 等于null 通常是最后一个章节{stopElement = null;OpenXmlElement tempElement = startElement;List<OpenXmlElement> openxmleList = new List<OpenXmlElement>();while (tempElement != stopElement){openxmleList.Add(tempElement);tempElement = tempElement.NextSibling();}foreach (var openXmlElement in openxmleList){openXmlElement.Remove();}}}}/// <summary>/// 创建纵向页分节符/// </summary>/// <returns></returns>public Paragraph CreatePortraitSectionBreakParagraph(){Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00052B73", RsidRunAdditionDefault = "00052B73" };ParagraphProperties paragraphProperties1 = new ParagraphProperties();SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00052B73" };//  PageSize pageSize1 = new PageSize() { Width = (UInt32Value)16838U, Height = (UInt32Value)11906U, Orient = PageOrientationValues.Landscape };PageSize pageSize1 = new PageSize();//PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U };DocumentFormat.OpenXml.Wordprocessing.Columns columns1 = new DocumentFormat.OpenXml.Wordprocessing.Columns() { Space = "425" };DocGrid docGrid1 = new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 };sectionProperties1.Append(pageSize1);//  sectionProperties1.Append(pageMargin1);sectionProperties1.Append(columns1);sectionProperties1.Append(docGrid1);paragraphProperties1.Append(sectionProperties1);paragraph1.Append(paragraphProperties1);return paragraph1;}/// <summary>/// 获取新段落/// </summary>/// <param name="text">段落文本</param>/// <returns>段落</returns>public Paragraph CreateParagraph(string text){Paragraph paragraph = new Paragraph();Run run = new Run();run.Append(new Text(text));paragraph.Append(run);return paragraph;}
}

2、新增标题并居中显示

 OpenXmlWordUtility wordUtility = new OpenXmlWordUtility();DocumentFormat.OpenXml.Wordprocessing.Paragraph par = this.WordUtility.CreateParagraph("1");  //新建标题1par.PrependChild(new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties());DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties pPr2 = par.ParagraphProperties;// 设置StyleId  if (pPr2.ParagraphStyleId == null)pPr2.ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId();pPr2.ParagraphStyleId.Val = "1";if (pPr2.Justification== null)pPr2.Justification= new DocumentFormat.OpenXml.Wordprocessing.Justification(){ Vall = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Center};this.WordUtility.Document.Body.Append(par);StyleDefinitionsPart styleDefinitionsPart1 = this.WordUtility.Document.MainDocumentPart.AddNewPart<StyleDefinitionsPart>("rId1");GeneratedCode.GeneratedClass.GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);this.WordUtility.SaveDocument();this.WordUtility.CloseDocument();

OpenXMl创建word并添加标题居中显示相关推荐

  1. Word如何添加标题

    Word如何添加标题 https://support.microsoft.com/zh-cn/office/%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98-3eb8b917- ...

  2. POI 写word,添加标题,表格,图片,自动生成目录,合并单元格

    工程地址:https://github.com/zheng-chang-wei/word package com.example.demo1.poi;import org.apache.poi.xwp ...

  3. Android普通对话框标题居中,android开发Toolbar标题居中显示的解决方法

    网上的一大堆,都是有问题不完整的,互相copy...记录一下自己的解决方法. android:id="@+id/tool_bar" android:layout_width=&qu ...

  4. gridview标题居中显示_Pr:制作片尾滚动字幕(旧版标题法)

    本文介绍使用 Pr 的旧版标题设计器来制作传统片尾滚动字幕的方法. 效果视频 ◆  ◆  ◆ 一般方法与步骤 1.首先,将文字内容输入到一个文本文件中.按 Ctrl/Cmd + A 全选,按 Ctrl ...

  5. python-docx生成word,插入图片居中显示问题解决办法

    具体生成word的代码官方示例在文末 下面只介绍图片居中的方法,因为docx库add_picture()方法不支持对图片位置的设置.所以有两种思路: 1.新建一个1行3列的表格,在中间的一列中插入图片 ...

  6. 在html中text按钮添加文字居中显示,html文字居中怎么设置

    方法:1.在标签中使用style属性,添加"text-align:center;"样式来设置文字水平居中:2.使用style属性,添加"vertical-align:mi ...

  7. Android ActionBar高级自定义——设置标题居中和添加控件

    转载地址:http://blog.csdn.net/gtbluesky/article/details/44656567 关于ActionBar的一些常见使用方法我已经在之前两篇博文(Android ...

  8. CSS 如何让li横向居中显示

    先给一个简单的示例HTML代码 [html] view plaincopy <body> <form id="form1" runat="server& ...

  9. js vue 设置excel单元格样式_vue项目使用xlsx-style实现前端导出Excel样式修改(添加标题,边框等),并且上传npm踩坑记录...

    前段时间,我们项目提出一个前端导出Excel表格的需求, 这个很简单,利用xlsx,file-saver很容易实现(网上很多教程). 后来需要加入标题,标题居中显示,加入边框等等样式需求,这就给我很多 ...

最新文章

  1. mybatis自动生成代码工具
  2. php对帖子分类,php – 从WordPress中的自定义分类获取所有帖子
  3. curl调用WEB API
  4. 百兆以太网传输距离_罗森伯格陪你做车载——汽车以太网(三)
  5. Linux运维基础(硬件和操作系统)
  6. 基于SpringBoot+Mybatis+Thymeleaf商品信息管理系统
  7. xss植入_前端安全之XSS攻击
  8. Windows服务卸载之后未重启机器之前再安装问题
  9. [Java] 蓝桥杯ALGO-62 算法训练 平方计算
  10. 17.2 无监督数据增强——UDA
  11. python 包的使用 (四)——Seaborn绘图
  12. 通信方式、通信接口、通信总线、通信协议的关系
  13. 红米ac2100有ipv6吗_【0107-多功能版OpenWrt】红米小米AC2100|IPV6|酸奶|SmartDNS|多拨|猫咪,附教程...
  14. 云计算关键概念之一:云资源池
  15. 客户机是计算机网络硬件吗,计算机网络中硬件连接设备有哪些?
  16. 淘宝订单同步方案 - 丢单终结者
  17. 查看、清空Linux日志【系统日志、软件运行日志】
  18. android官网m魅族15,魅族15系列亮相安卓官网 圆形Home键亮了
  19. Android开发:禁用应用分屏和屏幕旋转功能
  20. 配置一个nextjs_react项目

热门文章

  1. 三菱FX3G/FX3S 485协议通讯四台三菱E700变频器
  2. 英文文献的一些有意思的写法
  3. linux .sh文件详解,干货讲解之Linux Shell编程
  4. 在还原数据库出现[ERR] 1813 - Tablespace ,exists.错误
  5. hbase行健设计原则
  6. 如何添加自启动软件与关闭自启动软件
  7. csgo fps不稳定和服务器,急~CS1.6转向CSGO FPS不稳定问题咨询
  8. csgo卡住关不掉_csgo卡屏死机画面定格
  9. 【DIY小记】一种解决CSGO无法完整退出的方法
  10. python:返回列表和数组的最大最小值的索引