Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据输入一个部件_C#_Openxml_Wordprocessingml - Fatal编程技术网

C# 将数据输入一个部件

C# 将数据输入一个部件,c#,openxml,wordprocessingml,C#,Openxml,Wordprocessingml,我已经创建了一个word文档docx,现在正在使用OpenXml生产力工具对其进行“逆向工程”。我正在使用.FeedData()将样式、主题等输入到文档中,并将其保存为反射中的.xml文件——在我来到页脚之前,一切都进行得很顺利 以下是我为这些样式所做的工作(这非常好): StyleDefinitionsPart-StyleDefinitionsPart=mainPart.AddNewPart(); 使用(FileStream fs=newfilestream(Server.MapPath(“s

我已经创建了一个word文档docx,现在正在使用OpenXml生产力工具对其进行“逆向工程”。我正在使用
.FeedData()
将样式、主题等输入到文档中,并将其保存为反射中的.xml文件——在我来到页脚之前,一切都进行得很顺利

以下是我为这些样式所做的工作(这非常好):

StyleDefinitionsPart-StyleDefinitionsPart=mainPart.AddNewPart();
使用(FileStream fs=newfilestream(Server.MapPath(“styles.xml”)、FileMode.Open、FileAccess.Read))
{
样式定义spart.FeedData(fs);
}
查看我的文档,一切都在那里-现在我考虑页脚部分,将xml保存到footer.xml,并添加如下部分:

FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
using (FileStream fs = new FileStream(Server.MapPath("footer.xml"), FileMode.Open, FileAccess.Read))
{
    footerPart.FeedData(fs);
}
foreach (var section in mainPart.Document.Body.Elements<WP.SectionProperties>())
{
    section.PrependChild<WP.HeaderReference>(new WP.HeaderReference() { Id = mainPart.GetIdOfPart(headerPart) });
    section.PrependChild<WP.FooterReference>(new WP.FooterReference() { Id = mainPart.GetIdOfPart(footerPart) });
}
FooterPart FooterPart=mainPart.AddNewPart();
使用(FileStream fs=newfilestream(Server.MapPath(“footer.xml”)、FileMode.Open、FileAccess.Read))
{
footerPart.FeedData(fs);
}

其他一切看起来都很好,我可以看到文档中的部分,但是页脚没有出现在文档上,我在这里做错了什么?我需要告诉文档要使用哪个页脚部分还是什么吗?

已修复。在向正文中添加页脚和页眉之前,正文中似乎必须有一些内容,然后需要为每个部分添加对它们的引用,如下所示:

FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
using (FileStream fs = new FileStream(Server.MapPath("footer.xml"), FileMode.Open, FileAccess.Read))
{
    footerPart.FeedData(fs);
}
foreach (var section in mainPart.Document.Body.Elements<WP.SectionProperties>())
{
    section.PrependChild<WP.HeaderReference>(new WP.HeaderReference() { Id = mainPart.GetIdOfPart(headerPart) });
    section.PrependChild<WP.FooterReference>(new WP.FooterReference() { Id = mainPart.GetIdOfPart(footerPart) });
}
foreach(mainPart.Document.Body.Elements()中的var部分)
{
section.PrependChild(新的WP.HeaderReference(){Id=mainPart.GetIdOfPart(headerPart)});
section.PrependChild(新的WP.FooterReference(){Id=mainPart.GetIdOfPart(footerPart)});
}
记住最后添加页眉和页脚,这样文档中就有内容了

非常感谢这个答案: