gpt4 book ai didi

openxml - 打开 Xml : Word Create a new Document and add a paragraph from another document

转载 作者:行者123 更新时间:2023-12-02 05:34:01 26 4
gpt4 key购买 nike

我有一个文档,我正在通过获取段落进行迭代。对于这些段落中的每一个,我都需要创建一个新文档并保存它。我不知道如何将源文档中的段落添加到新文档中。

        foreach (var p in paragraphsFromSourceDocument)
{
using (var memoryStream = new MemoryStream())
{
var doc = WordprocessingDocument.Create(memoryStream, WordprocessingDocumentType.Document);
doc.AddMainDocumentPart();

// Create the Document DOM.
doc.MainDocumentPart.Document = new Document();
doc.MainDocumentPart.Document.Body = new Body();

//Add the paragraph 'p' to the Body here:
// HOW ?????????

doc.MainDocumentPart.Document.Save();
}
}

最佳答案

 // Open the file read-only since we don't need to change it.
using (var wordprocessingDocument = WordprocessingDocument.Open(documentFileName, true))
{
paragraphs = wordprocessingDocument.MainDocumentPart.Document.Body
.OfType<Paragraph>().ToList();
styles = wordprocessingDocument.MainDocumentPart.StyleDefinitionsPart;

foreach (var p in paragraphs)
{
using (var memoryStream = new MemoryStream())
{
var doc = WordprocessingDocument.Create(memoryStream, WordprocessingDocumentType.Document);
doc.AddMainDocumentPart().AddPart(styles);
doc.MainDocumentPart.Document = new Document();
doc.MainDocumentPart.Document.Body = new Body();
doc.MainDocumentPart.Document.Body.Append(p.CloneNode(true));
doc.MainDocumentPart.Document.Save();
Console.WriteLine(GetHTMLOfDoc(doc));

}
}
}

关于openxml - 打开 Xml : Word Create a new Document and add a paragraph from another document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com