在线预览Office文件【效果类似百度文库】_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 在线预览Office文件【效果类似百度文库】

在线预览Office文件【效果类似百度文库】

 2014/10/18 10:40:05  garfieldzf  程序员俱乐部  我要评论(0)
  • 摘要:引言结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好。提到Office文件在线预览,那么效果最好的应该就是百度文库的效果了,所以今天就忙里偷闲自己搞了下。用到知识点1、Office文件转化为Pdf文件。直接用.Net类库:Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Powerpoint
  • 标签:文件 百度 在线 Office 百度文库

引言 

       结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好。 提到Office文件在线预览,那么效果最好的应该就是百度文库的效果了,所以今天就忙里偷闲自己搞了下。

 

用到知识点

   1、Office文件转化为Pdf文件。直接用.Net类库:Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Powerpoint、Microsoft.Office.Interop.Word、Office。 我本机装的office2013,所以我选择的是12.0的。

   2、使用SwfTools将Pdf文件转化为Swf文件。

   3、使用众所周知的FlexPaper浏览Swf文件(预览时有水印,不知道怎么去掉)。

 

Demo过程中遇到的问题

   1、提示:"无法嵌入互操作类型Microsoft.Office.Interop.Word.ApplicationClass,请改用使用的接口"

        解决:右键Dll,嵌入互操作类型改为false即可。

   2、用到MsoTriState.msoTrue枚举类型参数时需要饮用Office.dll。 我一开始就没引用这个文件。

   3、生成Swf文件时需要传入完整的路径,我一开始只传入了路径,没有swf文件名,试了几次没成功。

 

效果图

 

转化代码

class="brush:csharp;gutter:true;">public class OfficeHelper
    {
        /// <summary>
        /// Word to Pdf
        /// </summary>
        /// <param name="srcFilePath"></param>
        /// <param name="targetFilePath"></param>
        /// <returns></returns>
        public static bool WordToPdf(string srcFilePath, string targetFilePath)
        {
            bool rs = false;
            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            Microsoft.Office.Interop.Word.ApplicationClass application = null;

            Microsoft.Office.Interop.Word.Document document = null;

            try
            {
                application = new Microsoft.Office.Interop.Word.ApplicationClass();
                application.Visible = false;
                document = application.Documents.Open(srcFilePath);
                document.SaveAs();
                document.ExportAsFixedFormat(targetFilePath, exportFormat);

                rs = true;
            }
            catch (Exception)
            {
                rs = false;
                throw;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                    document = null;
                }

                if (application != null)
                {
                    application.Quit();
                    application = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return rs;
        }

        /// <summary>
        /// Excel To Pdf
        /// </summary>
        /// <param name="srcFilePath"></param>
        /// <param name="targetFilePath"></param>
        /// <returns></returns>
        public static bool ExcelToPdf(string srcFilePath, string targetFilePath)
        {
            bool rs = false;
            Microsoft.Office.Interop.Excel.XlFixedFormatType exportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
            Microsoft.Office.Interop.Excel.ApplicationClass application = null;

            Microsoft.Office.Interop.Excel.Workbook document = null;

            try
            {
                application = new Microsoft.Office.Interop.Excel.ApplicationClass();
                application.Visible = false;
                document = application.Workbooks.Open(srcFilePath);
                document.SaveAs();
                document.ExportAsFixedFormat(exportFormat, targetFilePath);

                rs = true;
            }
            catch (Exception)
            {
                rs = false;
                throw;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                    document = null;
                }

                if (application != null)
                {
                    application.Quit();
                    application = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return rs;
        }

        /// <summary>
        /// PPT To Pdf
        /// </summary>
        /// <param name="srcFilePath"></param>
        /// <param name="targetFilePath"></param>
        /// <returns></returns>
        public static bool PptToPdf(string srcFilePath, string targetFilePath)
        {
            bool result;
            Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
            Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
            try
            {
                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

                persentation = application.Presentations.Open(srcFilePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetFilePath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary>
        /// Pdf To Swf
        /// </summary>
        /// <param name="swfTools">Swf转化工具路径</param>
        /// <param name="srcFilePath"></param>
        /// <param name="targetFilePath"></param>
        /// <returns></returns>
        public static bool PdfToSwf(string toolsPath, string cmd)
        {
            bool iss = false;//判断是否转换成功,默认失败
            try
            {
                using (Process p = new Process())
                {
                    ProcessStartInfo psi = new ProcessStartInfo(toolsPath, cmd);
                    p.StartInfo = psi;
                    p.Start();
                    p.WaitForExit();
                    iss = true;//转换成功
                }
            }
            catch { }
            return iss;
        }
    }

 

/// <summary>
        /// Pdf文件转化为Swf
        /// </summary>
        /// <param name="swfTools">转化工具路径</param>
        /// <param name="pdfPath">pdf文件目录</param>
        /// <param name="pdfFileName">pdf文件名</param>
        /// <param name="desPath">保存swf路径</param>
        /// <returns></returns>
        protected string PdfToSwf(string swfTools, string pdfPath, string pdfFileName, string desPath)
        {
            string fileFullName =Path.Combine(pdfPath,pdfFileName);
            string fileFullNameWithoutEx = Path.GetFileNameWithoutExtension(pdfFileName);
            string ext = Path.GetExtension(pdfFileName).ToLower();

            string saveSwfPath = desPath + fileFullNameWithoutEx + ".swf";
            string rs = fileFullNameWithoutEx + ".swf";
            
            string cmdStr = "  -t  \"" + fileFullName + "\" -s flashversion=9 -o \"" + saveSwfPath + "\"";
            bool iss = OfficeHelper.PdfToSwf(swfTools, cmdStr);

            return rs;
        }


/// <summary>
        /// Office文件转pdf文件
        /// </summary>
        /// <param name="officePath">office文件保存路径</param>
        /// <param name="officeFileName">office文件名</param>
        /// <param name="pdfPath">保存pdf路径</param>
        protected string OfficeToPdf(string officePath, string officeFileName, string pdfPath)
        {
            string fullPathName = Path.Combine(officePath, officeFileName);
            string fileNameWithoutEx = Path.GetFileNameWithoutExtension(officeFileName);
            string ext = Path.GetExtension(officeFileName).ToLower();

            string savePdfPath = pdfPath + fileNameWithoutEx + ".pdf";
            string retValue = fileNameWithoutEx + ".pdf";

            switch (ext)
            {
                case ".doc":
                    OfficeHelper.WordToPdf(fullPathName, savePdfPath);
                    break;
                case ".docx":
                    OfficeHelper.WordToPdf(fullPathName, savePdfPath);
                    break;
                case ".xls":
                    OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
                    break;
                case ".xlsx":
                    OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
                    break;
                case ".ppt":
                    OfficeHelper.PptToPdf(fullPathName, savePdfPath);
                    break;
                case ".pptx":
                    OfficeHelper.PptToPdf(fullPathName, savePdfPath);
                    break;
            }


            return retValue;
        }

 

参考

      在Demo的过程中,学习和参考了两位博友的文章,在此表示感谢

      Wolfy: http://www.cnblogs.com/wolf-sun/p/3569960.html

      静以修身:http://www.cnblogs.com/zzPrince/p/3378336.html

发表评论
用户名: 匿名