OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚
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文档 添加页、段落、页眉和页脚的更多相关文章
- C#实现通过模板自动创建Word文档的方法
原文地址:http://www.jb51.net/article/55332.htm 本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法 ...
- 在C#中创建word文档
在下面文档中 首先引用word组件:Microsoft.Office.Interop.Word 在头文件中写上 using Word = Microsoft.Office.Interop.Word; ...
- Java 后台创建word 文档
---恢复内容开始--- Java 后台创建 word 文档 自己总结 网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...
- [java,2017-05-04] 创建word文档
package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...
- Python批量创建word文档(2)- 加图片和表格
Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.要求在文档开始处给出banner条,价格日期等用表格表示.最后贴上自己的联系 ...
- OpenXml入门----给Word文档添加文字
使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...
- Python批量创建word文档(1)- 纯文字
Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.最后贴上自己的联系方式.代码如下: 1 ''' 2 #python根据需求新 ...
- 向Docx4j生成的word文档添加图片和布局--第一部分
原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...
- Java 如何给Word文档添加多行文字水印
前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...
随机推荐
- Linux安装和设置Samba服务器
1. 安装 安装前先关闭iptables和SELinux. Centos输入以下命令: yum install samba samba-client Ubuntu输入以下命令: apt-get ins ...
- 关于gitblit成功启动,但在阿里云外网地址无法访问的问题
1.配置/data/defaults.properties server.httpBindInterface= 此处什么都不要填空着就好. # Specify the interface for Je ...
- Java关闭Socket来终止线程
Java代码: package Threads; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...
- ThreadLocal MDC
因为MDC底层是用ThreadLocal实现的,所以这里补充一些和ThreadLocal相关的知识点. 1.ThreadLocal的三个层次 关于ThreadLocal有三个层次,可以按照这三个层次去 ...
- VBA遍历数组的2种方式
1.情景展示 VBA编程,如何对数组进行遍历? 2.解决方案 方式一:使用for循环 Sub 遍历数组1() '声明一个变量 Dim Arr As Variant '声明一个数字变量 Dim i ...
- POJ1013 称硬币
题目链接:http://poj.org/problem?id=1013 题目大意 有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三 ...
- Ubuntu18.04, WPS表格生成中文大写数字的script
=IF(ROUND(J9,2)<0,"无效数值",IF(ROUND(J9,2)=0,"零",IF(ROUND(J9,2)<1,"" ...
- 解决 Class not found和Base table or view not found: 1051 问题
1.解决class not found的方法: 如果你用的是homestead虚拟机,那么,你要到虚拟机下执行: composer dump-autoload 2.解决Base table or vi ...
- android.database.sqlite.SQLiteException: near "FROM"
07-20 00:19:30.496: E/JavaBinder(6807): *** Uncaught remote exception! (Exceptions are not yet su ...
- (原+转)win7上编译caffe支持python及matlab
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7126126.html 参考网址: https://github.com/happynear/caffe ...