C# 将Word文档转换为HTML

C# 将Word文档转换为HTML

分类: 学习总结 330人阅读 评论(2) 收藏 举报

       日常生活中,我们总是在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>  
C#代码:

  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>  
效果图:

转换后的Html文件

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

       日常生活中,我们总是在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>  
C#代码:

  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>  
效果图:

转换后的Html文件

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

猜你喜欢

转载自blog.csdn.net/sbayje/article/details/9011427