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文档时,有时也会碰到需要添加多行文 ...
随机推荐
- poj 1664 放苹果(递推)
题目链接:http://poj.org/problem? id=1664 放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 如何进入docker容器
http://blog.csdn.net/u010397369/article/details/41045251
- 不同版本的tomcat下载路径
1.由于安全问题,有些tomcat存在漏洞.为了升级要么修复漏洞,要么就直接升级tomcat. 一般升级tomcat比较省事.但是找到相应版本的tomcat比较难,所以还是要自己寻找对应的tomcat ...
- Cocos2d-x-Lua演示样例项目HelloLua
Cocos2d-x-Lua演示样例项目HelloLua 本篇博客介绍Cocos2d-x中Lua的实例项目,就是使用Cocos2d-x创建的初始项目执行所呈现的农场,这里笔者取名为HelloLua.本篇 ...
- python之模块datetime详解
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime详解 import datetime #data=datetime.dat ...
- Visual Studio 2015新添加宏
这个宏是类似环境变量,比如$(ProjectDir) $(SolutionDir),这样的,我需要新添加一个变量宏,但是VS的GUI上貌似找不到新的变量的设置,但是Qt的VS插件可以设置$(QTDIR ...
- 安卓高手之路之ClassLoader(三)
由于看C++和C代码看得很累,很辛苦.上一章终于解脱到java代码中来了. 第一个getClassLoader发生在main的preload方法中, public static void main(S ...
- 【Oracle】Oracle索引
在关系数据库中,索引是一种与表有关的数据库结构,它可以使对应于表的SQL语句执行得更快.索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容. 对于数据库来说,索引是一个必选项,但对于现 ...
- Digital Color Meter 颜色值提取工具
1.Digital Color Meter 简介 Digital Color Meter 是一款 Mac 自带的颜色值提取工具. 其它下载地址 Digital Color Meter for Mac, ...
- 【C语言】练习2-9
题目来源:<The C programming language>中的习题P38 练习2-9: 在求对二的补码时,表达式x &= (x-1)可以删除x中最右边值为1的一个二进 ...