当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 实现在线浏览word文档(word转html)

asp.net 实现在线浏览word文档(word转html)

2018年12月26日  | 移动技术网IT编程  | 我要评论

画蛇添足近义词,拜金女郎好看吗,roberta murgo

最近在做word文档在线浏览,找了种种方法、控件之后,回归到word转html,在线浏览....

一下是后台代码,前台html页面默认代码即可。


因为用文件如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.io;
using word = microsoft.office.interop.word;



using word = microsoft.office.interop.word;
没有引用好时,此处会报错,找不到interop之类的,

这时需要在引用里面引用组件.net下的microsoft.office.interop.visio,microsoft.office.interop.word


后台代码如下:


  protected void page_load(object sender, eventargs e)
        {
            string relativepath = request.querystring["filepath"]; //相对路径 ,从跳转页面得到文件相对路径。
            if (relativepath == "" || relativepath==null) return;
            string serverpath = server.mappath(relativepath);  //相对转服务器对应路径
            string html = serverpath.replace(".doc", ".html");

            if (!file.exists(@html)) //html页面不存在,把word转换成html
            {
                string filename = wordtohtml(serverpath);
                streamreader fread = new streamreader(filename, system.text.encoding.getencoding("gb2312"));
                string ss = fread.readtoend();
                response.write(ss); //直接写字符串到网页会发现,文字可显示,图片、表格无法显示。因此在后面重跳转到html文件页面。
                fread.close();
                fread.dispose();
            }

            html = relativepath.replace(".doc", ".html"); 
                               //html文件也存储在同样的路径下,
                              //只需要改了原路径的后缀即可得到html文件路径
            response.redirect(html);
            return;
        }

         ///  
        /// word转成html 
        ///  
        ///  
        private string wordtohtml(object wordfilename)
        {
            //在此处放置用户代码以初始化页面 
            word.application word = new word.application();
            type wordtype = word.gettype();
            word.documents docs = word.documents;
            //打开文件 
            type docstype = docs.gettype();
            word.document doc = (word.document)docstype.invokemember("open", system.reflection.bindingflags.invokemethod, null, docs, new object[] { wordfilename, true, true });
            //转换格式,另存为 
            type doctype = doc.gettype();
            string wordsavefilename = wordfilename.tostring();
            string strsavefilename = wordsavefilename.substring(0, wordsavefilename.length - 3) + "html";
            object savefilename = (object)strsavefilename;
            doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod, null, doc, new object[] { savefilename, word.wdsaveformat.wdformatfilteredhtml });
            doctype.invokemember("close", system.reflection.bindingflags.invokemethod, null, doc, null);
            //退出 word 
            wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod, null, word, null);
            return savefilename.tostring();
        } 


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网