我需要使用WordprocessingDocument创建新的OpenXMLDoc并向其中添加新的HTML内容。
到目前为止,我可以将HTML文本附加到现有文档中,但是我的要求是清除正文内容并将新的HTML内容添加到word doc
string str = CKEditor1.Text;
using (WordprocessingDocument doc = WordprocessingDocument.Open(Server.MapPath("~/Docs/Write.docx"), true))
{
string altChunkId = "myddId";
MainDocumentPart mainDocPart = doc.MainDocumentPart;
var run = new Run(new Text("test"));
var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new ParagraphProperties(
new Justification() { Val = JustificationValues.Center }),
run);
doc.MainDocumentPart.Document.RemoveAllChildren<BookmarkStart>();
doc.MainDocumentPart.Document.RemoveAllChildren<BookmarkEnd>();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<html><head></head><body>" + str + "</body></html>"));
AlternativeFormatImportPart formatImportPart =
mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, altChunkId);
formatImportPart.FeedData(ms);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainDocPart.Document.Body.Append(altChunk);
}
答案 0 :(得分:1)
我们只需要添加以下行,即可清除Word Doc中的所有内容
mainDocPart.Document.Body.RemoveAllChildren();