[英]OpenXML SpreadsheetDocument Header and Footer
我有以下WordprocessingDocument
代碼。 我可以使用 OpenXMl 編輯文檔並刪除然后添加回 header 和帶有更新的公司信息的頁腳。
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
做同樣的事情,但我不知道如何刪除/刪除現有的 header 和頁腳。 我將DeletePart
視為OpenXmlPartContainer
的一部分,它是OpenXmlPart: OpenXmlPartContainer
的一部分。 電子表格有 header 和頁腳,但我不知道如何訪問它們。
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);
}
}
檢查此代碼。 我剛剛對其進行了測試,它適用於電子表格。
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);
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.