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
  • 相关阅读:
    计算机网络杂烩
    网络体系、网络模型其他
    数据通信(系统)的物理要素
    数据通信历史
    没有备案的网站域名能解析吗
    dedecms列表页有图调用缩略图无图留空的方法
    dede 你所上传的软件类型不在许可列表,请更改系统对扩展名限定的配置
    跟版网 > 织梦教程 > 织梦安装使用 > 织梦DedeCMS附件上传大
    从#65279字符看dede模板页面编码问题
    sublime text如何保存为uft-8无bom编码格式文件
  • 原文地址:https://www.cnblogs.com/luvian/p/9317393.html
Copyright © 2011-2022 走看看