在c#中将Html转换为Docx

我想在c#中将html页面转换为docx,我该怎么做?

下面的内容与Luis代码相同,但只是更具可读性并应用于ASP.NET MVC应用程序:

var word = new Microsoft.Office.Interop.Word.Application(); word.Visible = false; var filePath = Server.MapPath("~/MyFiles/Html2PdfTest.html"); var savePathPdf = Server.MapPath("~/MyFiles/Html2PdfTest.pdf"); var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false); wordDoc.SaveAs2(FileName: savePathPdf, FileFormat: WdSaveFormat.wdFormatPDF); 

你也可以保存其他格式,如docx,如下所示:

 var savePathDocx = Server.MapPath("~/MyFiles/Html2PdfTest.docx"); var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false); wordDoc.SaveAs2(FileName: savePathDocx, FileFormat: WdSaveFormat.wdFormatXMLDocument); 

使用该代码进行转换

 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document(); Object oMissing = System.Reflection.Missing.Value; wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); word.Visible = false; Object filepath = "c:\\page.html"; Object confirmconversion = System.Reflection.Missing.Value; Object readOnly = false; Object saveto = "c:\\doc.pdf"; Object oallowsubstitution = System.Reflection.Missing.Value; wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); object fileFormat = WdSaveFormat.wdFormatPDF; wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing, ref oMissing); 

我的解决方案使用Html2OpenXml和DocumentFormat.OpenXml ( Html2OpenXml的NuGet包在这里 )为ASP.NET MVC提供了一个优雅的解决方案。

WordHelper.cs

 public static class WordHelper { public static byte[] HtmlToWord(String html) { const string filename = "test.docx"; if (File.Exists(filename)) File.Delete(filename); using (MemoryStream generatedDocument = new MemoryStream()) { using (WordprocessingDocument package = WordprocessingDocument.Create( generatedDocument, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); } HtmlConverter converter = new HtmlConverter(mainPart); Body body = mainPart.Document.Body; var paragraphs = converter.Parse(html); for (int i = 0; i < paragraphs.Count; i++) { body.Append(paragraphs[i]); } mainPart.Document.Save(); } return generatedDocument.ToArray(); } } } 

调节器

  [HttpPost] [ValidateInput(false)] public FileResult Demo(CkEditorViewModel viewModel) { return File(WordHelper.HtmlToWord(viewModel.CkEditorContent), "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); } 

我正在使用CKEditor为此示例生成HTML。

OpenXML SDK允许您以编程方式构建docx文档:

OpenXml SDK下载

您可以考虑使用altChunk。 请参阅将此图像添加到从altchunk创建的openxml doc中

如果您不想依赖Word转换HTML,可以尝试使用docx4j-ImportXHTML for .NET; 看这个演练 。

Aspose.Words for .NET是一个商业组件,可以实现这一目标。

MigraDoc可以提供帮助。 或者使用VS工具进行Office。 或通过COM连接到Office。

Microsoft不建议在Web服务器上使用Office应用程序。 但是使用OpenXML 2.5可以相当容易地完成

您所要做的就是将HTML分割为(“<”,“>”),然后将每个部分推送到交换机并确定它是否是HTML标记。

然后,对于每个部分,您可以开始将HTML转换为“Run”和“RunProperties”,并将非html文本简单地放入“Text”中

它听起来更难……是的我不知道为什么没有可用的代码可以做到这一点。

要记住的事情。 这两种格式并不能完全相互转换,所以如果你专注于最干净的代码,你就会遇到问题,因为它的格式会变得混乱。

您可以考虑使用PHPDocX ,它提供了一个非常方便的工具,可以将HTML文件和/或HTML字符串转换为WordML。

它有很多选择:

  1. 您可以使用CSS样式选择器进行过滤,应将哪些HTML块插入到Word文档中。
  2. 如果下载图像或将它们作为外部链接,您可以选择。
  3. 它解析HTML表单。
  4. 您可以使用本机Word样式来覆盖原始CSS的表和段落。
  5. 转换Word书签中的HTML锚点。
  6. 诸如此类

希望对你有帮助 :-)