0

我有以下代码WordprocessingDocument。我可以使用 OpenXMl 编辑文档,然后删除然后添加带有更新的公司信息的页眉和页脚。

using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(filestream, true))
{
    // Get the main document part
    MainDocumentPart mainDocumentPart = wdDoc.MainDocumentPart;

    // Delete the existing header and footer parts
    mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
    mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);

    // Create a new header part
    HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
    headerPart.Header = header;

    // Create header content
    // Code not included

    // Create a new footer part
    FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();

    // Create footer content
    // Code not included

    foreach (var section in sections)
    {
         // Delete existing references to headers and footers
         section.RemoveAllChildren<HeaderReference>();
         section.RemoveAllChildren<FooterReference>();

         // Create the new header and footer reference node
         section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId });
         section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId });

         PageMargin pageMargin = new PageMargin()
             {
                Top = 0,
                Right = (UInt32Value)504U,
                Bottom = 504,
                Left = (UInt32Value)504U,
                Header = (UInt32Value)360U,
                Footer = (UInt32Value)360U,
                Gutter = (UInt32Value)0U
             };

         section.Append(pageMargin);
     }

     mainDocumentPart.Document.Save();
     wdDoc.Close();
}

我正在尝试为 做同样的事情SpreadsheetDocument,但我不知道如何删除/删除现有的页眉和页脚。我认为DeletePartas 的一部分OpenXmlPartContainerwhich 是OpenXmlPart : OpenXmlPartContainer. 电子表格有页眉和页脚,但我不知道如何访问它们。

using (SpreadsheetDocument workbook = SpreadsheetDocument.Open(filestream, true))
{
     // Get the spreadsheet document parts
     WorkbookPart wbPart = workbook.WorkbookPart;
     WorksheetPart wsPart = wbPart.WorksheetParts.First();

     // Delete existing header
     Header header = wsPart.Worksheet.Descendants<Header>().FirstOrDefault();

     if (header != null)
     {
         wsPart.DeleteParts<Header>(header);
     }

     // Delete existing footer
     Footer footer = wsPart.Worksheet.Descendants<Footer>().FirstOrDefault();

     if (footer != null)
     {
         wsPart.DeleteParts<Footer>(footer);
     }
}
4

1 回答 1

0

检查此代码。我刚刚对其进行了测试,它适用于电子表格。

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;

namespace ExcelTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputPath = @"C:\PATH\Input.xlsx";
            string outputPath = @"C:\PATH\Output.xlsx";

            using (SpreadsheetDocument workbook = SpreadsheetDocument.Open(inputPath, true))
            {
                WorkbookPart workbookPart = workbook.WorkbookPart;
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.FirstOrDefault();
                Worksheet worksheet = worksheetPart.Worksheet;
                HeaderFooter header_footer = worksheet.Descendants<HeaderFooter>().FirstOrDefault();

                if (header_footer != null)
                {
                    var header = header_footer.FirstChild;
                    if (header != null)
                    {
                        header.Remove();
                    }

                    var footer = header_footer.LastChild;
                    if (footer != null)
                    {
                        footer.Remove();
                    }

                    workbook.SaveAs(outputPath);
                }
            }
        }
    }
}
于 2020-07-17T06:20:33.977 回答