2009-06-09 62 views
2

我正在寻找将html转换为MSWord的示例代码。 C#代码赞赏。 Html输入是一个字符串,其内容是html文档,我想学习如何使用.Net Word(Office)SDK来完成转换。将html转换为MSWord

由于事先 乔治

+0

这是[类似的问题](http://stackoverflow.com/questions/32151/best-way-to-export-html-to-word-without-having-ms-word-installed)George but the链接与我之前的回答相同t关于HTML编辑器的问题;-) – Shoban 2009-06-09 18:13:48

回答

4

这取决于你正试图转换的HTML文件的性质了很多。一种简单的方法是使用Word自动化来打开.html文档,然后将其保存为.doc文档。

 object readOnly = false; 
     object isVisible = true; 
     object missing = System.Reflection.Missing.Value; // Values we don't care about 
     object fileName = "C:/webpage.htm"; 
     object newFileName = "C:/webpage.doc";  

     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

     // word.Visible = true; // To see what's happening 

     Microsoft.Office.Interop.Word.Document document = word.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 

     document.Activate(); 

     object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; 

     document.SaveAs(ref newFileName, ref saveFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 

     document.Close(ref missing, ref missing, ref missing); 

  • 你必须增加的Microsoft.Office.Interop.Word的引用或类似的东西
  • 裁判失踪参数的数量取决于Word中的您至极版本使用
  • 由于Word实例从System文件夹开始,因此必须在文件名中使用完整路径。
0

请记住MSIE(或Word's,AFAIK MS Office仍然使用它自己的)渲染引擎并不像您希望的那样可靠,所以除简单格式之外的任何内容都可能看起来不同在Word文档中而不是在浏览器中。

Alsoplustoo,Word对DOC格式的解释可能与您的转换器有所不同 - OO.o有这个问题的年龄。