zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    JQuery empty方法和remove方法的区别,使用remove清除之前的文本内容??
    乙_1023 组个最小数 (20分)
    乙_1022 D进制的A+B (20分)
    乙_1021 个位数统计 (15分)
    乙_1020 月饼 (25分)
    乙_1013 数素数 (20分)
    乙_1009 说反话 (20分)
    乙_1008 数组元素循环右移问题 (20分)
    乙_1007 素数对猜想 (20分)
    乙_1005 继续(3n+1)猜想 (25分)
  • 原文地址:https://www.cnblogs.com/IT-Bear/p/3213850.html
Copyright © 2011-2022 走看看