.net 上传word 转为 html

简介: 前台:               word上传                       后台 /*上传类*///  public WordToHTML() { }         #region 上传文件并转换为html wordToHtml(wordFilePath)   ...

前台:



       <tr>
       <td align="center">word上传</td>
       <td> 
<asp:FileUpload runat="server" ID="File1" />
       </td>
       </tr>

后台


/*上传类*/
//  public WordToHTML() { }


        #region 上传文件并转换为html wordToHtml(wordFilePath)
        /// 
        /// 上传文件并转存为html
        /// 
        /// word文档在客户机的位置
        /// 上传的html文件的地址
        public string wordToHtml(System.Web.UI.WebControls.FileUpload wordFilePath)
        {
            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();


            //应当先把文件上传至服务器然后再解析文件为html
            string filePath = uploadWord(wordFilePath);


            //判断是否上传文件成功
            if (filePath == "0")
                return "0";
            //判断是否为word文件
            if (filePath == "1")
                return "1";


            object fileName = filePath;


            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 });
            doc.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;  //设置编码,以便ajax读取


            // 转换格式,另存为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();


            //被转换的html文档保存的位置
            string ConfigPath = HttpContext.Current.Server.MapPath("upload/" + 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 });


            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            //转到新生成的页面
            return ("upload/" + filename + ".html");
        }
        #endregion


        public string uploadWord(System.Web.UI.WebControls.FileUpload uploadFiles)
        {
            if (uploadFiles.PostedFile != null)
            {
                string fileName = uploadFiles.PostedFile.FileName;
                int extendNameIndex = fileName.LastIndexOf(".");
                string extendName = fileName.Substring(extendNameIndex);
                string newName = "";
                try
                {
                    //验证是否为word格式
                    if (extendName == ".doc" || extendName == ".docx")
                    {


                        DateTime now = DateTime.Now;
                        newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
                        //上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
                        uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
                    }
                    else
                    {
                        return "1";
                    }
                }
                catch
                {
                    return "0";
                }
                //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
                return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
            }


            else
            {
                return "0";
            }
        }


/*上传类*/



    protected void Button1_Click(object sender, EventArgs e)
    {
        var filenameurl = "";
        var File1 = FormView1.FindControl("File1") as FileUpload;
        try
        {
            //上传
            uploadWord(File1);
            //转换
            filenameurl = wordToHtml(File1);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            //Response.Write("恭喜,转换成功!");
        }

}

目录
相关文章
|
数据采集 存储 搜索推荐
用 Python 将 html 转为 pdf、word
在日常中有时需将 html 文件转换为 pdf、word 文件。网上免费的大多数不支持多个文件转换的情况,而且在转换几个后就开始收费了。
1091 0
用 Python 将 html 转为 pdf、word
|
5月前
html转word或pdf
html转word或pdf
84 0
|
5月前
|
XML 数据采集 JavaScript
基于.Net开源Html解析器,此外还支持SVG、XML等格式
基于.Net开源Html解析器,此外还支持SVG、XML等格式
23 0
|
5月前
|
Java
word转html
word转html
|
5月前
|
Java Apache
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
|
9月前
|
数据库
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
366 0
|
11月前
|
开发者
利用word制作html无法直接绘制的漂亮表格
利用word制作html无法直接绘制的漂亮表格
|
前端开发 程序员 开发工具
基于.Net开发的对比Html效果差异的开源项目
基于.Net 4.5开发的对比Html文件、片段效果差异的项目。两份Html效果不一样的地方会通过颜色、删除线、背景色分别标记出来。
177 0
基于.Net开发的对比Html效果差异的开源项目
|
JavaScript 前端开发 Linux
.Net开发的跨平台Word模板引擎
项目采用类似Vue、React模板方式,在模板定义相应的变量,再结合数据,快速生成Word文件。比如模板定义{FullName{}},项目会自动匹配键值为FullName的对象,并替换。
228 0
.Net开发的跨平台Word模板引擎
|
前端开发 Python
HTML通过Ajax上传图像至Django后端并转换为Mat图像
HTML通过Ajax上传图像至Django后端并转换为Mat图像