2011-03-25 76 views
12

我想将一个html页面转换为c#中的docx,我该怎么做?将Html转换为Docx中的c#

+1

http://stackoverflow.com/questions/32151/best-way-to-ex-port-html-to-word-without-having-ms-word-installed – 2011-03-25 11:11:46

+0

@PranayRana,这篇文章有比这更好的信息比你已经链接了一个。你有没有注意到这一点? – Rahul 2016-09-09 13:14:50

回答

4

使用的代码转换

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); 
0

Aspose.Words for .NET是一个商业组件,允许您实现这一点。

+0

使用Aspose [将HTML转换为Word文档]的示例(http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.loadformat.html)。 .NET的词语也可以查看。 – 2011-08-23 16:57:35

+1

ASPOSE从html转到docx时遇到了一些麻烦,比如造型和图片格式问题,这些问题对我来说似乎很基本,他们认为它们是产品限制... – Ariel 2012-05-24 16:10:12

+0

同意。缺乏对css的支持,即使是嵌入式css,也意味着您必须自己格式化所有表格,段落和列表。 – nullnvoid 2015-11-18 01:28:57

0

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

0

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

它有大量的选项,其中包括:

  1. 可以过滤使用其中的HTML块应该 被插入到Word文档中的CSS样式选择。
  2. 你可以选择下载图像或作为外部链接。
  3. 它解析HTML表单。
  4. 您可以将原生Word样式用于覆盖原始CSS的表格和段落。
  5. 转换Word书签中的HTML锚点。
  6. 诸如此类

我希望你觉得它有用:-)

7

下面做同样的事情路易斯代码,但只是有点更具可读性和适用于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); 
+1

记得调用'wordDoc.Close()'和'wordDoc.Quit()'来处理对象,否则你会留下背景中运行的单词的实例。 – 2016-07-26 13:25:51

2

我的解决方案使用Html2OpenXml以及DocumentFormat.OpenXmlNuGet package for Html2OpenXml is here)为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。

0

Microsoft不建议在Web服务器上使用Office应用程序。 然而,这可以很容易使用的OpenXML 2.5

所有你要真正做的是分裂的(“<”,‘>’)的HTML 然后为每个部分它推到一个开关,如果确定要做是否是HTML标记。

然后对于每个部分,你可以开始转换HTML为“运行”和“RunProperties”和非HTML文本被简单地放置到“文本”

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

需要注意的事项。 这两种格式不会完全相互转换,所以如果您将注意力集中在最干净的代码上,您会遇到自身变得混乱的格式问题。