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文档时,有时也会碰到需要添加多行文 ...
随机推荐
- tcmalloc asan
http://blog.csdn.net/jinzhuojun/article/details/46659155 http://blog.csdn.net/hanlizhong85/article/d ...
- Spring解决Hibernate中的懒加载问题
OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到. <filter> ...
- Yii2框架bootstrap样式理解
Yii2框架默认採用了bootstrap作为CSS风格,各种视图类组件都如此.之前一直採用默认风格,并在必要的时候加入或者改动一下class来达到目的.但在改版Yii1.1的orange项目时.发现之 ...
- Spring Boot动态修改日志级别
1. pom中引入 org.springframework.boot spring-boot-starter-actuator 2. 发送POST请求: 地址: http://[服务地址] ...
- ACE的源码划分
前几篇文章也提到过,ACE的所有源文件和头文件都杂乱堆在了ACE_wrappers/ace目录下.这样的代码组织方式给学习ACE带来了很大的困难,很多朋友在看到ace目录下庞大的代码的时候,几乎就失去 ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 解决FatalThrowableError in EloquentUserProvider.php line 126: Class '\App\User'问题
解决方法:http://stackoverflow.com/questions/28516454/laravel-5-user-model-not-found 总结:是因为我把app下的User移动到 ...
- ubuntu下安装万能五笔
之前一直习惯了万能五笔输入法,使用Ubuntu12.04安装wnwb 在网络上搜索了一下并没有找到可 以在ibus下直接使用万能五笔的方法,于是想自己动手解决这个问题, 参考别人之前做的万能五笔For ...
- How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6
Resolution Note: Transparent Huge Pages are not available on the 32-bit version of RHEL 6. Transpare ...
- git 错误信息 6 uncommitted changes would be overwritten by merge
表示本地库有几个文件修改没有提交,说明本地有几个文件没有跟踪