当前位置:首页 > C#教程 > C#高级

c#把word文档转换为html页面


首先找到一个引用,这个引用2015上的vs都有的,诺,就是这个!

using microsoft.office.interop.word

首选准备好你的word这里做测试呢 就在项目里面创建一个文件夹,给你要转换的word放到里面,

其次copy下面这段方法到你的项目里面

private string getpathbydoctohtml(string strfile)
        {
            if (string.isnullorempty(strfile))
            {
                return "0";//没有文件
            }

            microsoft.office.interop.word.applicationclass word = new microsoft.office.interop.word.applicationclass();
            type wordtype = word.gettype();
            microsoft.office.interop.word.documents docs = word.documents;

            // 打开文件
            type docstype = docs.gettype();

            object filename = strfile;

            microsoft.office.interop.word.document doc = (microsoft.office.interop.word.document)docstype.invokemember("open",
            system.reflection.bindingflags.invokemethod, null, docs, new object[] { filename, true, true });

            // 转换格式,另存为html
            type doctype = doc.gettype();
            //给文件重新起名
            string filename = system.datetime.now.year.tostring() + system.datetime.now.month.tostring() + system.datetime.now.day.tostring() +
            system.datetime.now.hour.tostring() + system.datetime.now.minute.tostring() + system.datetime.now.second.tostring();

            string strfilefolder = "/html/";
            datetime dt = datetime.now;
            //以yyyymmdd形式生成子文件夹名
            string strfilesubfolder = dt.year.tostring();
            strfilesubfolder += (dt.month < 10) ? ("0" + dt.month.tostring()) : dt.month.tostring();
            strfilesubfolder += (dt.day < 10) ? ("0" + dt.day.tostring()) : dt.day.tostring();
            string strfilepath = strfilefolder + strfilesubfolder + "/";
            // 判断指定目录下是否存在文件夹,如果不存在,则创建
            if (!directory.exists(server.mappath(strfilepath)))
            {
                // 创建up文件夹
                directory.createdirectory(server.mappath(strfilepath));
            }

            //被转换的html文档保存的位置
            // httpcontext.current.server.mappath("html" + strfilesubfolder + filename + ".html")
            string configpath = server.mappath(strfilepath + filename + ".html");
            object savefilename = configpath;

            /*下面是microsoft word 9 object library的写法,如果是10,可能写成:
              * doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
              * null, doc, new object[]{savefilename, word.wdsaveformat.wdformatfilteredhtml});
              * 其它格式:
              * wdformathtml
              * wdformatdocument
              * wdformatdostext
              * wdformatdostextlinebreaks
              * wdformatencodedtext
              * wdformatrtf
              * wdformattemplate
              * wdformattext
              * wdformattextlinebreaks
              * wdformatunicodetext
            */
            doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
            null, doc, new object[] { savefilename, microsoft.office.interop.word.wdsaveformat.wdformatfilteredhtml });

            //doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
            //  null, doc, new object[] { savefilename, microsoft.office.interop.word.wdsaveformat.wdformatfilteredhtml });

            //关闭文档
            doctype.invokemember("close", system.reflection.bindingflags.invokemethod,
            null, doc, new object[] { null, null, null });

            // 退出 word
            wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod, null, word, null);
            //转到新生成的页面
            //return ("/" + filename + ".html");

            //转化html页面统一编码格式
            transhtmlencoding(configpath);

            return (strfilepath + filename + ".html");
        }
        private void transhtmlencoding(string strfilepath)
        {
            try
            {
                system.io.streamreader sr = new system.io.streamreader(strfilepath, encoding.getencoding(0));
                string html = sr.readtoend();
                sr.close();
                html = system.text.regularexpressions.regex.replace(html, @"<meta[^>]*>", "<meta http-equiv=content-type content='text/html; charset=gb2312'>", system.text.regularexpressions.regexoptions.ignorecase);
                system.io.streamwriter sw = new system.io.streamwriter(strfilepath, false, encoding.default);

                sw.write(html);
                sw.close();
            }
            catch (exception ex)
            {
                page.clientscript.registerstartupscript(page.clientscript.gettype(), "myscript", "<script>alert('" + ex.message + "')</script>");
            }
        }

其实是两个方法。

为了测试呢 你需要随便弄个地方调用这个这个方法

string strword = server.mappath("/wordpath/thisisword.doc");
getpathbydoctohtml(strword);
就是这个,需要把你的word传进去,当然你可以写在page_load里面,测试用运行之后打开你的项目找到你保存的位置就行了。

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!