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权限的用户即可

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

部分CS源码:

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

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

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

 68ExpandedSubBlockEnd.gif        #endregion

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

 89InBlock.gif                    else
 90ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 91InBlock.gif                        return "1";
 92ExpandedSubBlockEnd.gif                    }

 93ExpandedSubBlockEnd.gif                }

 94InBlock.gif                catch
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 96InBlock.gif                    return "0";
 97ExpandedSubBlockEnd.gif                }

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

101InBlock.gif
102InBlock.gif            else
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                return "0";
105ExpandedSubBlockEnd.gif            }

106ExpandedSubBlockEnd.gif        }

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

117InBlock.gif            catch (Exception ex)
118ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
119InBlock.gif                throw ex;
120ExpandedSubBlockEnd.gif            }

121InBlock.gif            finally
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
123InBlock.gif                Response.Write("恭喜,转换成功!");
124ExpandedSubBlockEnd.gif            }

125ExpandedSubBlockEnd.gif        }

126InBlock.gif
127InBlock.gif

word文件中前几个笑话:

1、 碗掉下来,天大个疤

 2、一次文艺晚会,主持人上台报幕:下面请欣赏:新疆歌舞,掀起你的头盖骨!  

毛骨悚然!!!!!

3、老虎不发猫,你当我是病危呀!  

4、上高中时,课堂纪律混乱,老师一怒之下揪起XXX,说:XXX,你给我站墙上去!  

~~全班暴寒!

5、一次我开车,坐我旁边的女同事突然问:“你怎么开车不系安全套的?”

 6、我:那是我们物理老师。。。

还有30多个都在word文档中

  完整项目源码下载

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值