我正在使用这种方法生成docx
文件:
public static void CreateDocument(string documentFileName, string text)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body><w:p><w:r><w:t>#REPLACE#</w:t></w:r></w:p></w:body>
</w:document>";
docXml = docXml.Replace("#REPLACE#", text);
using (Stream stream = mainPart.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
stream.Write(buf, 0, buf.Length);
}
}
}
它就像一个魅力:
CreateDocument("test.docx", "Hello");
但是如果我想放置 HTML 内容而不是Hello
? 例如:
CreateDocument("test.docx", @"<html><head></head>
<body>
<h1>Hello</h1>
</body>
</html>");
或者是这样的:
CreateDocument("test.docx", @"Hello<BR>
This is a simple text<BR>
Third paragraph<BR>
Sign
");
这两种情况都会为document.xml
. 任何想法?如何从 HTML 内容生成 docx 文件?