C#实现HTML转WORD及WORD转PDF的方法

2019-12-26 13:37:47王旭
  • System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, false, false });  // 转换格式,另存为 
  • Type docType = doc.GetType();  object saveFileName = "d:Reportsaaa.doc"; 
  • //下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:  /* 
  • docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,  null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML}); 
  • */  ///其它格式: 
  • ///wdFormatHTML  ///wdFormatDocument 
  • ///wdFormatDOSText  ///wdFormatDOSTextLineBreaks 
  • ///wdFormatEncodedText  ///wdFormatRTF 
  • ///wdFormatTemplate  ///wdFormatText 
  • ///wdFormatTextLineBreaks  ///wdFormatUnicodeText 
  • docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,  null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatDocument }); 
  • // 退出 Word  wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, 
  • null, word, null);  } 
  • private void WordConvert(string s)  { 
  • oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();  Type wordType = word.GetType(); 
  • //打开WORD文档  /*对应脚本中的 
  • var word = new ActiveXObject("Word.Application");  var doc = word.Documents.Open(docfile); 
  • */  oWord.Documents docs = word.Documents; 
  • Type docsType = docs.GetType();  object objDocName =s; 
  • oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });  //打印输出到指定文件 
  • //你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数  Type docType = doc.GetType(); 
  • object printFileName = @"c:aaa.ps";  docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });