2011-03-25 81 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文本被簡單地放置到「文本」

這聽起來更難那麼它是...是的,我不知道爲什麼沒有可用的代碼來做到這一點。

需要注意的事項。 這兩種格式不會完全相互轉換,所以如果您將注意力集中在最乾淨的代碼上,您會遇到自身變得混亂的格式問題。