zoukankan      html  css  js  c++  java
  • word转html预览

      #region Index页面
            /// <summary>
            /// Index页面
            /// </summary>
            /// <paramname="url">例:/uploads/......XXX.xls</param>
            public ActionResult PreviewWord()
            {
                string url =Request.QueryString["url"];
                //物理路径
                string physicalPath = Server.MapPath(Server.UrlDecode(url));
                string extension = Path.GetExtension(physicalPath);
    
                string htmlUrl = "";
                switch (extension.ToLower())
                {
                    case ".doc":
                    case ".docx":
                        htmlUrl = PreviewWordView(physicalPath, url);
                        break;
    
                }
                switch (extension.ToLower())
                {
                    case ".pdf":
                        htmlUrl = physicalPath;
                        break;
    
                }
    
                return Redirect(Url.Content(htmlUrl));
            }
            #endregion
    
            #region 预览word文档(doc转html版)
            public string PreviewWordView(string physicalPath,string url)
            {
                Microsoft.Office.Interop.Word._Application application = null;
                Microsoft.Office.Interop.Word._Document doc = null;
                application = new Microsoft.Office.Interop.Word.Application();
                object missing = Type.Missing;
                object trueObject = true;
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing,
                       missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                //保存为html
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
                String outputFile = Path.GetDirectoryName(physicalPath) + "\" + htmlName;
                doc.SaveAs(outputFile, format, missing, missing, missing,
                                      missing, XlSaveAsAccessMode.xlNoChange, missing,
                                      missing, missing, missing, missing);
                //关闭文档对象
                doc.Close();
                //关闭应用程序对象
                application.Quit();
                //杀进程,有的情况下,关闭word文档会不成功,会残留很多word进程
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
                foreach (System.Diagnostics.Process p in processes)
                {
                    bool b = p.MainWindowTitle == "";
                    if (p.MainWindowTitle == "")
                    {
                        p.Kill();
                    }
                }
                return Path.GetDirectoryName(Server.UrlDecode(url))+"\"+htmlName;
            }
    
            #endregion
  • 相关阅读:
    vim lua对齐indent无效
    C中的私有成员
    Lua 设置table为只读属性
    c语言结构体可以直接赋值
    Lua5.3 注册表 _G _ENV
    火狐浏览器调试ajax异步页面时报错NS_ERROR_UNEXPECTER
    ajax向后台请求数据,后台接收到数据并进行了处理,但前台就是调用error方法
    maven安装之后,或者升级之后遇到的问题:could not find or load main class org.codehaus.plexus.class.....
    jenkins执行shell命令,有时会提示“Command not found”
    shell 脚本替换文件中某个字符串
  • 原文地址:https://www.cnblogs.com/luvian/p/9317393.html
Copyright © 2011-2022 走看看