2016-07-25 3 views
1

Я использую Microsoft.Office.Interop.Word для преобразования html в word.Я создаю html-файл на локальной машине, а затем преобразовываю его в файл слова. Но файл слова не показывает правильное форматирование. Он просто показывает изображения. Я ссылался на вопросы о стеке. Но мне не повезло. Мой образец HTML файл, как показано ниже: -Преобразование html в слово 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; 

Edit: -После исследовать дальше, я обнаружил, что слово становится созданное .Но это не в состоянии изменить фон с помощью радиальных градиентов .а там какой-нибудь другой способ, чтобы добавить радиальный фон?

ответ

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

Смежные вопросы