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
作者:唐小熊
出处:http://www.cnblogs.com/IT-Bear/
关于作者:一头写代码的熊
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
如有问题,可以通过kumat@foxmail.com 联系我,非常感谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构