using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.docx", WordprocessingDocumentType.Document))
{
MainDocumentPart objMainDocumentPart = objWordDocument.AddMainDocumentPart();
Document objDocument = new Document();
Body objBody = new Body();
objDocument.Append(objBody);
objMainDocumentPart.Document = objDocument;
}

转自 http://blog.csdn.net/songpengpeng20100202/article/details/7071030

1. 创建word文档作为模版

2. 以下代码实现需求

using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml.Wordprocessing; namespace OpenXmlAddParagraphsToNewPage
{
class Program
{
static void Main(string[] args)
{
if (File.Exists("copy.docx"))
{
File.Delete("copy.docx");
} File.Copy("InsertNewPageAndParagraphs.docx", "copy.docx");
using (WordprocessingDocument doc = WordprocessingDocument.Open("copy.docx", true))
{
var body = doc.MainDocumentPart.Document.Body; Paragraph newPara = new Paragraph(new Run
(new Break() { Type = BreakValues.Page },
new Text("text on the new page"))); body.Append(newPara); AddHeader(doc);
AddFooter(doc);
doc.MainDocumentPart.Document.Save();
}
} private static void AddFooter(WordprocessingDocument doc)
{
// Declare a string for the header text.
string newFooterText =
"New footer via Open XML Format SDK 2.0 classes"; // Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart; // Delete the existing footer parts.
mainDocPart.DeleteParts(mainDocPart.FooterParts); // Create a new footer part and get its relationship id.
FooterPart newFooterPart = mainDocPart.AddNewPart<FooterPart>();
string rId = mainDocPart.GetIdOfPart(newFooterPart); // Call the GeneratePageFooterPart helper method, passing in
// the footer text, to create the footer markup and then save
// that markup to the footer part.
GeneratePageFooterPart(newFooterText).Save(newFooterPart); // Loop through all section properties in the document
// which is where footer references are defined.
foreach (SectionProperties sectProperties in
mainDocPart.Document.Descendants<SectionProperties>())
{
// Delete any existing references to footers.
foreach (FooterReference footerReference in
sectProperties.Descendants<FooterReference>())
sectProperties.RemoveChild(footerReference); // Create a new footer reference that points to the new
// footer part and add it to the section properties.
FooterReference newFooterReference =
new FooterReference() { Id = rId, Type = HeaderFooterValues.Default };
sectProperties.Append(newFooterReference);
} // Save the changes to the main document part.
mainDocPart.Document.Save();
} private static void AddHeader(WordprocessingDocument doc)
{
// Declare a string for the header text.
string newHeaderText =
"New header via Open XML Format SDK 2.0 classes"; // Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart; // Delete the existing header parts.
mainDocPart.DeleteParts(mainDocPart.HeaderParts); // Create a new header part and get its relationship id.
HeaderPart newHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
string rId = mainDocPart.GetIdOfPart(newHeaderPart); // Call the GeneratePageHeaderPart helper method, passing in
// the header text, to create the header markup and then save
// that markup to the header part.
GeneratePageHeaderPart(newHeaderText).Save(newHeaderPart); // Loop through all section properties in the document
// which is where header references are defined.
foreach (SectionProperties sectProperties in
mainDocPart.Document.Descendants<SectionProperties>())
{
// Delete any existing references to headers.
foreach (HeaderReference headerReference in
sectProperties.Descendants<HeaderReference>())
sectProperties.RemoveChild(headerReference); // Create a new header reference that points to the new
// header part and add it to the section properties.
HeaderReference newHeaderReference =
new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default };
sectProperties.Append(newHeaderReference);
} // Save the changes to the main document part.
//mainDocPart.Document.Save(); } // Creates an header instance and adds its children.
private static Header GeneratePageHeaderPart(string HeaderText)
{
// set the position to be the center
PositionalTab pTab = new PositionalTab()
{
Alignment = AbsolutePositionTabAlignmentValues.Center,
RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin,
Leader = AbsolutePositionTabLeaderCharValues.None
}; var element =
new Header(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Header" }),
new Run(pTab,
new Text(HeaderText))
)
); return element;
} // Creates an Footer instance and adds its children.
private static Footer GeneratePageFooterPart(string FooterText)
{
PositionalTab pTab = new PositionalTab()
{
Alignment = AbsolutePositionTabAlignmentValues.Center,
RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin,
Leader = AbsolutePositionTabLeaderCharValues.None
}; var elment =
new Footer(
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "Footer" }),
new Run(pTab,
new Text(FooterText))
)
);
return elment;
}
}
}

3. 运行效果

转自 http://blog.csdn.net/songpengpeng20100202/article/details/7071030

OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚的更多相关文章

  1. C#实现通过模板自动创建Word文档的方法

    原文地址:http://www.jb51.net/article/55332.htm   本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法 ...

  2. 在C#中创建word文档

    在下面文档中  首先引用word组件:Microsoft.Office.Interop.Word 在头文件中写上 using Word = Microsoft.Office.Interop.Word; ...

  3. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  4. [java,2017-05-04] 创建word文档

    package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...

  5. Python批量创建word文档(2)- 加图片和表格

    Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.要求在文档开始处给出banner条,价格日期等用表格表示.最后贴上自己的联系 ...

  6. OpenXml入门----给Word文档添加文字

    使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...

  7. Python批量创建word文档(1)- 纯文字

    Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.最后贴上自己的联系方式.代码如下: 1 ''' 2 #python根据需求新 ...

  8. 向Docx4j生成的word文档添加图片和布局--第一部分

    原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...

  9. Java 如何给Word文档添加多行文字水印

    前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...

随机推荐

  1. spring的jdbc

    Spring将替我们完成所有使用JDBC API进行开发的单调乏味的.底层细节处理工作. 操作JDBC时Spring可以帮我们做这些事情: 定义数据库连接参数,打开数据库连接,处理异常,关闭数据库连接 ...

  2. Mybatis-Generator自动生成XML文件以及接口和实体类

    整合了MySQL和Oracle配置文件生成方法 这个是整个文件夹的下载地址:http://www.codepeople.cn/download 主要给大家介绍一下generatorConfig.xml ...

  3. ORA-01950: no privileges on tablespace xxx ORA-01950: 对表空间 'xxx'无权限

    场景: 创建用户,在用户scheme下新建了一张表,插入数据时报错 ORA-01950: 对表空间 'xxx'无权限 创建用户 /*第1步:创建临时表空间 */ create temporary ta ...

  4. HttpServletResponse对象(二)

    一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,

  5. java第七节 IO

    /* *第七讲 IO/输入与输出 * * File类 * RandomAccessFile类 * 各种节点流类 * 字符编码 * 各种过滤流与包装类 * *File类 * File类是IO包中唯一代表 ...

  6. spring boot mybatis没有扫描jar中的Mapper接口

    只需要在spring boot启动类上加上注解,并指定jar包中接口文件包路径即可 如下: @ComponentScan(basePackages = "com.xx") @Map ...

  7. <转>SQL 左外连接,右外连接,全连接,内连接

    本文节选自:https://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html       连接条件可在FROM或WHERE子句中指 ...

  8. realloc 使用详解(分析realloc invalid pointer、指针无效等错误)【转】

    来源:http://www.cnblogs.com/ladd/archive/2012/06/30/2571420.htmlrealloc函数用来为ptr重新分配大小为size的一块内存,看似很简单, ...

  9. DHCP工作过程的六个主要步骤

    对于学习DHCP来说,很重要的一部分就是对于DHCP工作过程的理解. DHCP分为两个部分:一个是服务器端,另一个是客户端. 所有客户机的IP地址设定资料都由DHCP服务器集中管理,并负责处理客户端的 ...

  10. 树莓派进阶之路 (015) - 树莓派使用DS18B20模块测量温度

    参考:http://shumeipai.nxez.com/2013/10/03/raspberry-pi-temperature-sensor-monitors.html 第一步,允许单总线接口 su ...