2016-07-25 148 views
1

我正在使用Microsoft.Office.Interop.Word将html转换为word.I在本地机器上创建html文件,然后将其转换为word文件。但是word文件未显示正确的格式。它只是显示图像。我提到了stackoverflow的问题,但我发现没有运气。 我的样本HTML文件,如下图所示: - 如下图所示将html转换为word c#

<!doctype html> 
 
<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'> 
 

 
<head> 
 
    <title="Device Information" /> 
 
    <style> 
 
    table.main { 
 
     width: 700px; 
 
     height: 450px; 
 
     background: radial-gradient(#1367AB 0%, #154075 60%); 
 
    } 
 
    td.firsttablerow1 { 
 
     width: 190px; 
 
    } 
 
    td.firsttablerow2 { 
 
     width: auto; 
 
     color: white; 
 
    } 
 
    tr.image1 { 
 
     margin: "15px,20px,30px,40px"; 
 
     align-items: stretch; 
 
     height: 100px; 
 
    } 
 
    tr.image2 { 
 
     margin: "0px,20px,30px,40px"; 
 
     align-items: stretch; 
 
    } 
 
    tr.text1 { 
 
     color: #DDE9F2; 
 
     align-items: flex-end; 
 
     font-size: 9; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <table align="center" class="main"> 
 
    <tr> 
 
     <td class="firsttablerow1"> 
 
     <table> 
 
      <tr class="image1"> 
 
      <td> 
 
       <img src='C:\Jellyfish.jpg' width='200' height='90'> 
 
      </td> 
 
      </tr> 
 
      <tr class="image2"> 
 
      <td> 
 
       <img src='C:\Desert.jpg' width='150' height='90'> 
 
      </td> 
 
      </tr> 
 
      <tr class="text1"> 
 
      <td>"abc"</td> 
 
      </tr> 
 
     </table> 
 
     </td> 
 
     <td class="firsttablerow2">"xyz"</td> 
 
    </tr> 
 
    </table> 
 

 
</body> 
 

 
</html>

我的C#代码将HTML转换成字。

MSWord.Application word = new MSWord.Application { Visible = false }; 
     word.Documents.Open("htmlfilename", Format: WdOpenFormat.wdOpenFormatWebPages); 
     MSWord.Document doc = word.Documents[@"htmlfilename"]; 
     doc.SaveAs2(@"wordFileName", WdSaveFormat.wdFormatDocument); 
     doc.Close(); 
     doc = null; 
     word.Quit(); 
     word = null; 

编辑: - 后进一步调查,我发现这个词是越来越。但它是无法使用径向渐变。是有任何其他方式增加径向背景改变的背景产生的?

回答

0

可以试试这个

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

我想这个代码,但我得到的显示相同output.only图像。 – Alex