隐藏

C# 将Word文档转换为HTML

发布:2020/3/2 21:21:57作者:管理员 来源:本站 浏览次数:1081

日常生活中,我们总是在Word中进行文字的编辑,它不仅能够保存Text文本,还可以保存文本的格式等等。那么如果我要将一Word文档上的内容展示在网页上,该怎么做呢?这里我提供了一个小工具,你可以将Word转换为Html,需要显示的话,可以直接访问该Html,废话不多说,下面看代码。

页面代码:

  1. <SPAN style="FONT-SIZE: 18px"><div>  
  2.     <input id="File1" type="file" runat="server"/>  
  3.     <asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" />  
  4. </div></SPAN>  

  1. <div>
  2. <input id="File1" type="file" runat="server"/>
  3. <asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" />
  4. </div>
C#代码:
[csharp] view plaincopy
  1. <SPAN style="FONT-SIZE: 18px">using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Collections.Generic;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.Security;  
  9. using System.Web.UI;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Web.UI.HtmlControls;  
  13. using System.IO;  
  14.   
  15. protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         /// <summary>   
  21.         /// 将word转换为Html   
  22.         /// </summary>   
  23.         /// <param name="sender"></param>   
  24.         /// <param name="e"></param>   
  25.         protected void btnConvert_Click(object sender, EventArgs e)  
  26.         {  
  27.             try  
  28.             {  
  29.                   
  30.                 //上传   
  31.                 //uploadWord(File1);   
  32.                 //转换   
  33.                 wordToHtml(File1);  
  34.             }  
  35.             catch (Exception ex)  
  36.             {  
  37.                 throw ex;  
  38.             }  
  39.             finally  
  40.             {  
  41.                 Response.Write("恭喜,转换成功!");  
  42.             }  
  43.   
  44.         }  
  45.   
  46.         //上传文件并转换为html wordToHtml(wordFilePath)   
  47.         ///<summary>   
  48.         ///上传文件并转存为html   
  49.         ///</summary>   
  50.         ///<param name="wordFilePath">word文档在客户机的位置</param>   
  51.         ///<returns>上传的html文件的地址</returns>   
  52.         public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)  
  53.         {  
  54.             Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();  
  55.             Type wordType = word.GetType();  
  56.             Microsoft.Office.Interop.Word.Documents docs = word.Documents;  
  57.   
  58.             // 打开文件   
  59.             Type docsType = docs.GetType();  
  60.   
  61.             //应当先把文件上传至服务器然后再解析文件为html   
  62.             string filePath = uploadWord(wordFilePath);  
  63.   
  64.             //判断是否上传文件成功   
  65.             if (filePath == "0")  
  66.                 return "0";  
  67.             //判断是否为word文件   
  68.             if (filePath == "1")  
  69.                 return "1";  
  70.   
  71.             object fileName = filePath;  
  72.   
  73.             Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",  
  74.             System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, truetrue });  
  75.   
  76.             // 转换格式,另存为html   
  77.             Type docType = doc.GetType();  
  78.   
  79.             string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +  
  80.             System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();  
  81.   
  82.             // 判断指定目录下是否存在文件夹,如果不存在,则创建   
  83.             if (!Directory.Exists(Server.MapPath("~\\html")))  
  84.             {  
  85.                 // 创建up文件夹   
  86.                 Directory.CreateDirectory(Server.MapPath("~\\html"));  
  87.             }  
  88.   
  89.             //被转换的html文档保存的位置   
  90.             string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");  
  91.             object saveFileName = ConfigPath;  
  92.   
  93.             /*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成: 
  94.          * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 
  95.          * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML}); 
  96.          * 其它格式: 
  97.          * wdFormatHTML 
  98.          * wdFormatDocument 
  99.          * wdFormatDOSText 
  100.          * wdFormatDOSTextLineBreaks 
  101.          * wdFormatEncodedText 
  102.          * wdFormatRTF 
  103.          * wdFormatTemplate 
  104.          * wdFormatText 
  105.          * wdFormatTextLineBreaks 
  106.          * wdFormatUnicodeText 
  107.          */  
  108.             docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,  
  109.             null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });  
  110.   
  111.             //关闭文档   
  112.             docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,  
  113.             null, doc, new object[] { nullnullnull });  
  114.   
  115.             // 退出 Word   
  116.             wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);  
  117.             //转到新生成的页面   
  118.             return ("/" + filename + ".html");  
  119.   
  120.         }  
  121.   
  122.   
  123.         public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)  
  124.         {  
  125.             if (uploadFiles.PostedFile != null)  
  126.             {  
  127.                 string fileName = uploadFiles.PostedFile.FileName;  
  128.   
  129.                 int extendNameIndex = fileName.LastIndexOf(".");  
  130.                 string extendName = fileName.Substring(extendNameIndex);  
  131.                 string newName = "";  
  132.                 try  
  133.                 {  
  134.                     //验证是否为word格式   
  135.                     if (extendName == ".doc" || extendName == ".docx")  
  136.                     {  
  137.   
  138.                         DateTime now = DateTime.Now;  
  139.                         newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();  
  140.   
  141.                         // 判断指定目录下是否存在文件夹,如果不存在,则创建   
  142.                         if (!Directory.Exists(Server.MapPath("~\\wordTmp")))  
  143.                         {  
  144.                             // 创建up文件夹   
  145.                             Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));  
  146.                         }  
  147.   
  148.                         //上传路径 指当前上传页面的同一级的目录下面的wordTmp路径   
  149.                         uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));  
  150.                     }  
  151.                     else  
  152.                     {  
  153.                         return "1";  
  154.                     }  
  155.                 }  
  156.                 catch  
  157.                 {  
  158.                     return "0";  
  159.                 }  
  160.                 //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;   
  161.                 return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);  
  162.             }  
  163.             else  
  164.             {  
  165.                 return "0";  
  166.             }  
  167.         }</SPAN>  

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. using System.IO;
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. }
  17. /// <summary>
  18. /// 将word转换为Html
  19. /// </summary>
  20. /// <param name="sender"></param>
  21. /// <param name="e"></param>
  22. protected void btnConvert_Click(object sender, EventArgs e)
  23. {
  24. try
  25. {
  26. //上传
  27. //uploadWord(File1);
  28. //转换
  29. wordToHtml(File1);
  30. }
  31. catch (Exception ex)
  32. {
  33. throw ex;
  34. }
  35. finally
  36. {
  37. Response.Write("恭喜,转换成功!");
  38. }
  39. }
  40. //上传文件并转换为html wordToHtml(wordFilePath)
  41. ///<summary>
  42. ///上传文件并转存为html
  43. ///</summary>
  44. ///<param name="wordFilePath">word文档在客户机的位置</param>
  45. ///<returns>上传的html文件的地址</returns>
  46. public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
  47. {
  48. Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
  49. Type wordType = word.GetType();
  50. Microsoft.Office.Interop.Word.Documents docs = word.Documents;
  51. // 打开文件
  52. Type docsType = docs.GetType();
  53. //应当先把文件上传至服务器然后再解析文件为html
  54. string filePath = uploadWord(wordFilePath);
  55. //判断是否上传文件成功
  56. if (filePath == "0")
  57. return "0";
  58. //判断是否为word文件
  59. if (filePath == "1")
  60. return "1";
  61. object fileName = filePath;
  62. Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
  63. System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
  64. // 转换格式,另存为html
  65. Type docType = doc.GetType();
  66. string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
  67. System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
  68. // 判断指定目录下是否存在文件夹,如果不存在,则创建
  69. if (!Directory.Exists(Server.MapPath("~\\html")))
  70. {
  71. // 创建up文件夹
  72. Directory.CreateDirectory(Server.MapPath("~\\html"));
  73. }
  74. //被转换的html文档保存的位置
  75. string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
  76. object saveFileName = ConfigPath;
  77. /*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
  78. * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
  79. * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
  80. * 其它格式:
  81. * wdFormatHTML
  82. * wdFormatDocument
  83. * wdFormatDOSText
  84. * wdFormatDOSTextLineBreaks
  85. * wdFormatEncodedText
  86. * wdFormatRTF
  87. * wdFormatTemplate
  88. * wdFormatText
  89. * wdFormatTextLineBreaks
  90. * wdFormatUnicodeText
  91. */
  92. docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
  93. null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
  94. //关闭文档
  95. docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
  96. null, doc, new object[] { null, null, null });
  97. // 退出 Word
  98. wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
  99. //转到新生成的页面
  100. return ("/" + filename + ".html");
  101. }
  102. public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
  103. {
  104. if (uploadFiles.PostedFile != null)
  105. {
  106. string fileName = uploadFiles.PostedFile.FileName;
  107. int extendNameIndex = fileName.LastIndexOf(".");
  108. string extendName = fileName.Substring(extendNameIndex);
  109. string newName = "";
  110. try
  111. {
  112. //验证是否为word格式
  113. if (extendName == ".doc" || extendName == ".docx")
  114. {
  115. DateTime now = DateTime.Now;
  116. newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
  117. // 判断指定目录下是否存在文件夹,如果不存在,则创建
  118. if (!Directory.Exists(Server.MapPath("~\\wordTmp")))
  119. {
  120. // 创建up文件夹
  121. Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));
  122. }
  123. //上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
  124. uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
  125. }
  126. else
  127. {
  128. return "1";
  129. }
  130. }
  131. catch
  132. {
  133. return "0";
  134. }
  135. //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
  136. return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
  137. }
  138. else
  139. {
  140. return "0";
  141. }
  142. }
效果图:

转换后的Html文件

       这样就可以简单的在Html中展示word文档中的内容,而不需要在自己进行编辑了。当然,如果有需要的话,可以将转换的Html的路径存入数据库,根据不同的条件直接进行访问。