zoukankan      html  css  js  c++  java
  • PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法:

    1.通过办公软件dll转换,用flans去看

    2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js)

    今天我解决的就是WORD/EXCEL/PPT 转化成 PDF ,然后 PDF在线阅读

    1.WORD/PPT/EXCEL转PPT类(很简单的)

    using Aspose.Words;
    using Aspose.Cells;
    using Aspose.Slides;
    
    /// <summary>
    /// Office2Pdf 将Office文档转化为pdf
    /// </summary>
    public class Office2Pdf
    {
        public Office2Pdf()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false; 
            try
            {
                Document doc = new Document(sourcePath);
                doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {        
            }
            return result;
        }
    
        /// <summary>
        /// 把Excel文件转换成PDF格式文件  
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public bool XLSConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
          
            try
            {
                //Excel
                Workbook excel = new Workbook(sourcePath);
                excel.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
              
            }
            return result;
        }
        ///<summary>        
        /// 把PowerPoint文件转换成PDF格式文件       
        ///</summary>        
        ///<param name="sourcePath">源文件路径</param>     
        ///<param name="targetPath">目标文件路径</param> 
        ///<returns>true=转换成功</returns> 
        public bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            try
            {
                //PPT
                Presentation ppt = new Presentation(sourcePath);
                ppt.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {        
            }
            return result;
        }
    }
    View Code

    2.做一个上传页面(核心代码)

            private string upFile()
            {
                int iTotal = Request.Files.Count;
    
                if (iTotal == 0)
                {
                    _msg = "没有数据";
                }
                else
                {
                    HttpPostedFile file = Request.Files[0];
                    string path = "file\" + DateTime.Now.ToString("yyyy-MM-dd") + "\";
                    string viewPath = "PDF\web\" + path;
                    string ArticlePath = System.Web.HttpContext.Current.Server.MapPath("~") + viewPath;
                    if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                    {
                        //建立图片主文件夹
                        if (!Directory.Exists(ArticlePath))
                        {
                            Directory.CreateDirectory(ArticlePath);
                        }
                        saveName = Path.GetFileName(file.FileName);
                        string extension = Path.GetExtension(file.FileName).ToLower();
                        string fileName = DateTime.Now.ToString("HH-mm-ss") + extension;
                        ArticlePath += fileName;
                        //保存文件
                        file.SaveAs(ArticlePath);
                        string pdfpath = ArticlePath.Substring(0, ArticlePath.Length - extension.Length) + ".pdf";
                        if (extension == ".doc" || extension == ".docx")
                        {
                            office2pdf.DOCConvertToPDF(ArticlePath, pdfpath);
                        }
                        else if (extension == ".ppt" || extension == ".pptx")
                        {
                            office2pdf.PPTConvertToPDF(ArticlePath, pdfpath);
                        }
                        else if (extension == ".xls" || extension == ".xlsx")
                        {
                            office2pdf.XLSConvertToPDF(ArticlePath, pdfpath);
                        }
                        else if (extension == ".pdf")
                        {
                        }
                        else
                        {
                            this.RegisterStartupScript("newWindow", "<script language='javascript'>alert('非法文件!')</script>");
                            return "";
                        }
                        DelFile(ArticlePath);
                        savePath = path + fileName.Substring(0, fileName.Length - extension.Length) + ".pdf";
                        return savePath;
                    }
                }
                return "";
    
            }
    View Code

    3.效果如下

    PPT    :

    WORD:

    4. DEMO: 下载 (新手上路,希望大家多多指点  )

  • 相关阅读:
    CF 990A. Commentary Boxes【数学/模拟】
    HDU 变形课 1181【DFS/BFS】
    HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
    牛客网 小白赛4 A三角形【贪心】
    POJ 2251 Dungeon Master【三维BFS模板】
    Poj 1236 Network of Schools (Tarjan)
    Poj 2186 Popular Cows (Tarjan)
    Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
    搜索专题题解
    Light 1289
  • 原文地址:https://www.cnblogs.com/0to9/p/6083179.html
Copyright © 2011-2022 走看看