亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

添加頁腳后打開的 XML 文檔無法讀取

添加頁腳后打開的 XML 文檔無法讀取

C#
侃侃無極 2023-07-09 16:16:30
我正在嘗試使用下面的代碼在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);    }
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您需要doc.Close();在方法結束時調用Insert。這將保存并關閉所有底層流。您可以刪除對 的調用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();

    }

}


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號