zoukankan      html  css  js  c++  java
  • C#实现上传WORD文档并另存为WEB页面

    在开始之前,需要注意的一点,需要引用MICROSOFT WORD 这个COM组件,很多开发者都安装了 OFFICE并且也能找到这个COM,可是在引用后编译时仍然会出现无法找到依赖项的错误提示,这是因为你装.net framework 之前装了office 2003,所以在装office 2003的时候,不会自动安装“Office 2003 主 Interop 程序集”。

    解决方法

             安装“Office 2003 主 Interop 程序集”-它属于office 2003 的可选组件,如有office 2003 安装程序或者光盘,可再次运行安装程序,选择“添加或删除组件”,->勾选“高级自定义应用程序”,->展开特定于应用程序的节点。例如,要获取 Microsoft Office word 2003 PIA,请展开 Microsoft Office word 节点并选择 .NET Programmability Support(.net可编程支持)。单击 .NET Programmability Support (.net可编程支持)旁边的下拉箭头以选择更新选项,并选取 Run from My Computer(从本机运行)。然后点击“更新”按钮。即可!!!

    后台代码

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    public partial class testWtoH : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
     
        //public WordToHTML() { }
     
        //上传文件并转换为html wordToHtml(wordFilePath)
        ///<summary>
        ///上传文件并转存为html
        ///</summary>
        ///<param name="wordFilePath">word文档在客户机的位置</param>
        ///<returns>上传的html文件的地址</returns>
        public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile 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 });
     
            // 转换格式,另存为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("html/" + 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("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");
        }
     
     
        public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile 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")
                    {
     
                        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 btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                //上传
                //uploadWord(File1);
                //转换
                wordToHtml(File1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Response.Write("恭喜,转换成功!");
            }
        }
    }
     
    前台代码

      

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testWtoH.aspx.cs" Inherits="testWtoH" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="File1" type="file" runat="server" />
     
             <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传转换" />
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    openoffice centos7.4 安装
    zabbix 监控wind登录状态
    centos7.4 nfs-2.3.2
    zabbix 监控iptables
    docker端口映射和容器互相访问
    docker端口映射和容器互相访问
    PostgreSQL unlogged表
    docker数据卷学习-利用数据卷实现mysql的快速恢复和迁移
    利用docker搭建本地私有镜像仓库
    docker安装详解
  • 原文地址:https://www.cnblogs.com/ZetaChow/p/2237364.html
Copyright © 2011-2022 走看看