Asp.net2.0实现Word转换Html

 前两天在园子里看到了 BlackSoul 写的asp.net将word转换为html保存 》的文章,觉得很实用,但是搭建项目时候缺不是那么回事儿,中间有很多问题,比如说word组建的引用、web.config权限的设置,看到了很多人在网上也在问这个问题,我把问题一一解决,测试成功,现在把全部项目源码文件分享给大家下载。
项目截图:
 
   功能概述及注意事项:
该源码主要是通过Asp.net2.0实现Word文档上传并自动转换为Html文件,原理是将word文档上传至服务器然后再转存为html格式文件,再解析html文件修改其页面样式和css。

wordTmp为上传是word暂存文件夹
html为转换后html保存文件夹

注意:请设置web.config中的<identity impersonate="true" userName="administrator" password="51aspx"/>帐号和密码,否则会提示检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。

该用户类型为拥有user权限的用户即可

  示例Word文件是些笑话,希望能博得大家多多笑容!

部分CS源码:

  1      public  partial  class  _Default : System.Web.UI.Page
  2      {
  3      //  public WordToHTML() { }
  4
  5        #region 上传文件并转换为html wordToHtml(wordFilePath)
  6        /// 
  7        /// 上传文件并转存为html
  8        /// 
  9        /// word文档在客户机的位置
 10        /// 上传的html文件的地址

 11        public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
 12        {
 13            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
 14            Type wordType = word.GetType();
 15            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
 16
 17            // 打开文件
 18            Type docsType = docs.GetType();
 19
 20            //应当先把文件上传至服务器然后再解析文件为html
 21            string filePath = uploadWord(wordFilePath);
 22
 23            //判断是否上传文件成功
 24            if (filePath == "0")
 25                return "0";
 26            //判断是否为word文件
 27            if (filePath == "1")
 28                return "1";
 29
 30            object fileName = filePath;
 31
 32            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
 33            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, truetrue });
 34
 35            // 转换格式,另存为html
 36            Type docType = doc.GetType();
 37
 38            string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
 39            System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
 40
 41            //被转换的html文档保存的位置
 42            string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
 43            object saveFileName = ConfigPath;
 44
 45            /*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
 46            * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
 47            * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
 48            * 其它格式:
 49            * wdFormatHTML
 50            * wdFormatDocument
 51            * wdFormatDOSText
 52            * wdFormatDOSTextLineBreaks
 53            * wdFormatEncodedText
 54            * wdFormatRTF
 55            * wdFormatTemplate
 56            * wdFormatText
 57            * wdFormatTextLineBreaks
 58            * wdFormatUnicodeText
 59            */

 60            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
 61            null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
 62
 63            // 退出 Word
 64            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
 65            //转到新生成的页面
 66            return ("/" + filename + ".html");
 67        }

 68        #endregion

 69
 70        public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
 71        {
 72            if (uploadFiles.PostedFile != null)
 73            {
 74                string fileName = uploadFiles.PostedFile.FileName;
 75                int extendNameIndex = fileName.LastIndexOf(".");
 76                string extendName = fileName.Substring(extendNameIndex);
 77                string newName = "";
 78                try
 79                {
 80                    //验证是否为word格式
 81                    if (extendName == ".doc")
 82                    {
 83
 84                        DateTime now = DateTime.Now;
 85                        newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
 86                        //上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
 87                        uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
 88                    }

 89                    else
 90                    {
 91                        return "1";
 92                    }

 93                }

 94                catch
 95                {
 96                    return "0";
 97                }

 98                //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
 99                return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
100            }

101
102            else
103            {
104                return "0";
105            }

106        }

107
108        protected void btnUpload_Click(object sender, EventArgs e)
109        {
110            try
111            {
112                //上传
113                uploadWord(File1);
114                //转换
115                wordToHtml(File1);
116            }

117            catch (Exception ex)
118            {
119                throw ex;
120            }

121            finally
122            {
123                Response.Write("恭喜,转换成功!");
124            }

125        }

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值