添加页脚后打开 XML 文档无法读取
Open XML document unreadable after adding footer
我正在尝试使用以下代码在 word 文档中添加页脚。该文件正在生成,但当我尝试打开该文件时,它显示该文档不可读的消息。我不知道我在这里做错了什么。
WordprocessingDocument doc;
Body docBody;
public void Insert()
{
doc = WordprocessingDocument.Create(@"d:\report1.docx", WordprocessingDocumentType.Document);
docBody = new Body();
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
mainPart.Document.Body = docBody;
ApplyFooter();
doc.Save();
}
public void ApplyFooter()
{
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
FooterPart footerPart1 = mainDocPart.AddNewPart<FooterPart>("r98");
Footer footer1 = new Footer();
Paragraph paragraph1 = new Paragraph() { };
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footer stuff";
run1.Append(text1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
footerPart1.Footer = footer1;
SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault();
if (sectionProperties1 == null)
{
sectionProperties1 = new SectionProperties() { };
mainDocPart.Document.Body.Append(sectionProperties1);
}
FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" };
sectionProperties1.InsertAt(footerReference1, 0);
}
您需要在 Insert
方法结束时调用 doc.Close();
。这将保存并关闭任何底层流。您可以删除对 doc.Save()
.
的调用
使用 using
语句可能更简洁,它会为您调用 Close
:
WordprocessingDocument doc;
Body docBody;
public void Insert()
{
using (doc = WordprocessingDocument.Create(@"d:\report1.docx", WordprocessingDocumentType.Document))
{
Body docBody = new Body();
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
mainPart.Document.Body = docBody;
ApplyFooter();
}
}
我正在尝试使用以下代码在 word 文档中添加页脚。该文件正在生成,但当我尝试打开该文件时,它显示该文档不可读的消息。我不知道我在这里做错了什么。
WordprocessingDocument doc;
Body docBody;
public void Insert()
{
doc = WordprocessingDocument.Create(@"d:\report1.docx", WordprocessingDocumentType.Document);
docBody = new Body();
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
mainPart.Document.Body = docBody;
ApplyFooter();
doc.Save();
}
public void ApplyFooter()
{
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
FooterPart footerPart1 = mainDocPart.AddNewPart<FooterPart>("r98");
Footer footer1 = new Footer();
Paragraph paragraph1 = new Paragraph() { };
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footer stuff";
run1.Append(text1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
footerPart1.Footer = footer1;
SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault();
if (sectionProperties1 == null)
{
sectionProperties1 = new SectionProperties() { };
mainDocPart.Document.Body.Append(sectionProperties1);
}
FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" };
sectionProperties1.InsertAt(footerReference1, 0);
}
您需要在 Insert
方法结束时调用 doc.Close();
。这将保存并关闭任何底层流。您可以删除对 doc.Save()
.
使用 using
语句可能更简洁,它会为您调用 Close
:
WordprocessingDocument doc;
Body docBody;
public void Insert()
{
using (doc = WordprocessingDocument.Create(@"d:\report1.docx", WordprocessingDocumentType.Document))
{
Body docBody = new Body();
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
mainPart.Document.Body = docBody;
ApplyFooter();
}
}