我使用this library将HTML转换为DOCX:
private static void ConvertToDocx(string html)
{
using (MemoryStream generatedDocument = new MemoryStream())
{
using (
WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument,
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.MainDocumentPart;
if (mainPart == null)
{
mainPart = package.AddMainDocumentPart();
var doc = new Document(new Body());
doc.Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
converter.ParseHtml(html);
mainPart.Document.Save();
}
File.WriteAllBytes("file.docx", generatedDocument.ToArray());
}
}
它就像一个魅力,但它总是生成固定大小的文件,例如3KB。生成的文件似乎工作,我可以在MS Word中打开它。但它并没有在我的应用程序中加载control(RichTextEditor)。有没有我忘记在ConvertToDocx
方法中添加的东西?