转载:http://www.cnblogs.com/yxlblogs/p/4139167.html

引言

结合上个项目和目前做的这个项目,其中都用到了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文件名,试了几次没成功。

效果图

转化代码

  1 public class OfficeHelper
  2     {
  3         /// <summary>
  4         /// Word to Pdf
  5         /// </summary>
  6         /// <param name="srcFilePath"></param>
  7         /// <param name="targetFilePath"></param>
  8         /// <returns></returns>
  9         public static bool WordToPdf(string srcFilePath, string targetFilePath)
 10         {
 11             bool rs = false;
 12             Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
 13             Microsoft.Office.Interop.Word.ApplicationClass application = null;
 14
 15             Microsoft.Office.Interop.Word.Document document = null;
 16
 17             try
 18             {
 19                 application = new Microsoft.Office.Interop.Word.ApplicationClass();
 20                 application.Visible = false;
 21                 document = application.Documents.Open(srcFilePath);
 22                 document.SaveAs();
 23                 document.ExportAsFixedFormat(targetFilePath, exportFormat);
 24
 25                 rs = true;
 26             }
 27             catch (Exception)
 28             {
 29                 rs = false;
 30                 throw;
 31             }
 32             finally
 33             {
 34                 if (document != null)
 35                 {
 36                     document.Close();
 37                     document = null;
 38                 }
 39
 40                 if (application != null)
 41                 {
 42                     application.Quit();
 43                     application = null;
 44                 }
 45
 46                 GC.Collect();
 47                 GC.WaitForPendingFinalizers();
 48             }
 49
 50             return rs;
 51         }
 52
 53         /// <summary>
 54         /// Excel To Pdf
 55         /// </summary>
 56         /// <param name="srcFilePath"></param>
 57         /// <param name="targetFilePath"></param>
 58         /// <returns></returns>
 59         public static bool ExcelToPdf(string srcFilePath, string targetFilePath)
 60         {
 61             bool rs = false;
 62             Microsoft.Office.Interop.Excel.XlFixedFormatType exportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
 63             Microsoft.Office.Interop.Excel.ApplicationClass application = null;
 64
 65             Microsoft.Office.Interop.Excel.Workbook document = null;
 66
 67             try
 68             {
 69                 application = new Microsoft.Office.Interop.Excel.ApplicationClass();
 70                 application.Visible = false;
 71                 document = application.Workbooks.Open(srcFilePath);
 72                 document.SaveAs();
 73                 document.ExportAsFixedFormat(exportFormat, targetFilePath);
 74
 75                 rs = true;
 76             }
 77             catch (Exception)
 78             {
 79                 rs = false;
 80                 throw;
 81             }
 82             finally
 83             {
 84                 if (document != null)
 85                 {
 86                     document.Close();
 87                     document = null;
 88                 }
 89
 90                 if (application != null)
 91                 {
 92                     application.Quit();
 93                     application = null;
 94                 }
 95
 96                 GC.Collect();
 97                 GC.WaitForPendingFinalizers();
 98             }
 99
100             return rs;
101         }
102
103         /// <summary>
104         /// PPT To Pdf
105         /// </summary>
106         /// <param name="srcFilePath"></param>
107         /// <param name="targetFilePath"></param>
108         /// <returns></returns>
109         public static bool PptToPdf(string srcFilePath, string targetFilePath)
110         {
111             bool result;
112             Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
113             object missing = Type.Missing;
114             Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
115             Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
116             try
117             {
118                 application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
119
120                 persentation = application.Presentations.Open(srcFilePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
121                 persentation.SaveAs(targetFilePath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
122
123                 result = true;
124             }
125             catch (Exception e)
126             {
127                 Console.WriteLine(e.Message);
128                 result = false;
129             }
130             finally
131             {
132                 if (persentation != null)
133                 {
134                     persentation.Close();
135                     persentation = null;
136                 }
137                 if (application != null)
138                 {
139                     application.Quit();
140                     application = null;
141                 }
142                 GC.Collect();
143                 GC.WaitForPendingFinalizers();
144                 GC.Collect();
145                 GC.WaitForPendingFinalizers();
146             }
147             return result;
148         }
149
150         /// <summary>
151         /// Pdf To Swf
152         /// </summary>
153         /// <param name="swfTools">Swf转化工具路径</param>
154         /// <param name="srcFilePath"></param>
155         /// <param name="targetFilePath"></param>
156         /// <returns></returns>
157         public static bool PdfToSwf(string toolsPath, string cmd)
158         {
159             bool iss = false;//判断是否转换成功,默认失败
160             try
161             {
162                 using (Process p = new Process())
163                 {
164                     ProcessStartInfo psi = new ProcessStartInfo(toolsPath, cmd);
165                     p.StartInfo = psi;
166                     p.Start();
167                     p.WaitForExit();
168                     iss = true;//转换成功
169                 }
170             }
171             catch { }
172             return iss;
173         }
174     }

 1 /// <summary>
 2         /// Pdf文件转化为Swf
 3         /// </summary>
 4         /// <param name="swfTools">转化工具路径</param>
 5         /// <param name="pdfPath">pdf文件目录</param>
 6         /// <param name="pdfFileName">pdf文件名</param>
 7         /// <param name="desPath">保存swf路径</param>
 8         /// <returns></returns>
 9         protected string PdfToSwf(string swfTools, string pdfPath, string pdfFileName, string desPath)
10         {
11             string fileFullName =Path.Combine(pdfPath,pdfFileName);
12             string fileFullNameWithoutEx = Path.GetFileNameWithoutExtension(pdfFileName);
13             string ext = Path.GetExtension(pdfFileName).ToLower();
14
15             string saveSwfPath = desPath + fileFullNameWithoutEx + ".swf";
16             string rs = fileFullNameWithoutEx + ".swf";
17
18             string cmdStr = "  -t  \"" + fileFullName + "\" -s flashversion=9 -o \"" + saveSwfPath + "\"";
19             bool iss = OfficeHelper.PdfToSwf(swfTools, cmdStr);
20
21             return rs;
22         }
23
24
25 /// <summary>
26         /// Office文件转pdf文件
27         /// </summary>
28         /// <param name="officePath">office文件保存路径</param>
29         /// <param name="officeFileName">office文件名</param>
30         /// <param name="pdfPath">保存pdf路径</param>
31         protected string OfficeToPdf(string officePath, string officeFileName, string pdfPath)
32         {
33             string fullPathName = Path.Combine(officePath, officeFileName);
34             string fileNameWithoutEx = Path.GetFileNameWithoutExtension(officeFileName);
35             string ext = Path.GetExtension(officeFileName).ToLower();
36
37             string savePdfPath = pdfPath + fileNameWithoutEx + ".pdf";
38             string retValue = fileNameWithoutEx + ".pdf";
39
40             switch (ext)
41             {
42                 case ".doc":
43                     OfficeHelper.WordToPdf(fullPathName, savePdfPath);
44                     break;
45                 case ".docx":
46                     OfficeHelper.WordToPdf(fullPathName, savePdfPath);
47                     break;
48                 case ".xls":
49                     OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
50                     break;
51                 case ".xlsx":
52                     OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
53                     break;
54                 case ".ppt":
55                     OfficeHelper.PptToPdf(fullPathName, savePdfPath);
56                     break;
57                 case ".pptx":
58                     OfficeHelper.PptToPdf(fullPathName, savePdfPath);
59                     break;
60             }
61
62
63             return retValue;
64         }

参考

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

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

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

源代码:http://yunpan.cn/cAhzwWhy5bgVD (提取码:7900)

或者下载地址为: http://files.cnblogs.com/files/jbps/Show.Office2swf.zip

转载于:https://my.oschina.net/jiboping/blog/795944

在线预览Office文件【效果类似百度文库】(转载)相关推荐

  1. 在线预览office文件

    通过微软公开的api接口,将文档的URL传入即可实现在线预览office文件,而不需要去下载文件. 同时,若是想做提供预览office办公软件的服务,直接调用接口即可,无需利用openoffice或者 ...

  2. 微软接口在线预览office文件

    通过微软公开的api接口,将文档的URL传入即可实现在线预览office文件,而不需要去下载文件. 同时,若是想做提供预览office办公软件的服务,直接调用接口即可,无需利用openoffice或者 ...

  3. 经管资源库项目总结----在线预览office文件的实现与总结

    依旧是这个经管的项目.在线预览作为资源和文档管理系统的一个很酷的并且是如此重要的功能,是必须要实现的.然后百度一下office在线预览,看起来so eazy啊,各种博客各种demo,一下子就做出效果来 ...

  4. vue实现在线预览office文件

    最近在做电子档案,后端提供了文件的华为云的oss链接.已经实现了点击下载文件的功能.但是呢,他们又希望常规的文件,可以直接点击预览,不需要下载. 按道理说,做文件的在线预览,买个第三方服务什么的,后端 ...

  5. js在线预览office文件的示例代码

    方法一: 用微软的office online进行在线预览 https://view.officeapps.live.com/op/view.aspx?src=文件地址 只能查看 'doc', 'doc ...

  6. 微信小程序 - 在线预览 Office 文件(doc / docx / xls / xlsx / ppt / pptx / pdf)

    效果图 前言 网上大部分教程功能有问题且文章无逻辑混乱,本文将提供优秀的示例. 本文只适用于预览 服务端接口返回的网络地址文件,"本地上传" 文件并预览原理一样, 例如服务端接口返 ...

  7. C#中mvc模式在线预览Office(word 可编辑、txt)文件

    /// <summary>/// 在线预览Office文件/// </summary>public class OfficeViewController : Controlle ...

  8. springboot使用pdfjs预览office文件

    由于springboot使用aspose预览office文件可以实现文件预览,但部分浏览器却不兼容,所以使用pdfjs预览office文件,兼容浏览器. 在springboot使用aspose预览of ...

  9. web项目使用OpenOffice实现前端在线预览office文档(超详细)

    超详细的OpenOffice实现前端在线预览office文档记录 最近搞一个数字化共享平台,是一个java web项目,使用框架ssm,其中项目有一个需要在线预览PDF.excle.ppt.word文 ...

最新文章

  1. Gartner:人工智能将改变个人设备领域的游戏规则
  2. python版本与编码的区别
  3. 临床、实验室和流行病学研究的样本量 Sample Sizes for Clinical, Laboratory and Epidemiology Studies
  4. 人工智能们再也不用担心撞上玻璃橱窗了
  5. asp:DropDownList用法
  6. SAP Spartacus和product相关的标准normalizer
  7. 软件开发中 前台、中台、后台英文_最近处处惹人爱的中台到底是什么
  8. 华为服务器默认什么系统,云服务器默认系统
  9. tkinter中text属性_python tkinter基本属性详解
  10. jquery 处理json字符串
  11. 计算机数据传输和信号传输,职称计算机基础知识第1章:数据传输的编码和调制技术...
  12. Android Sutiod报错:Dx unsupported class file version 52.0(解决)
  13. windows如何设定定时关机和取消定时
  14. Java程序员简历书写
  15. 利用计算机做实验报告,计算机应用实验报告样本.doc
  16. 图片视频音频开源文件转换器file-converter
  17. java 第七章 数组
  18. 黑猴子的家:SVN 服务端创建资料库
  19. 复制mysql 搬家_MYSQL快速搬家心得
  20. 笔记:全网最详细jQuery教程

热门文章

  1. ceph提示: non-power-of-two pg_num解决办法
  2. BUAA 计网mooc测试题3-网络层
  3. 企业JUNIPER-SSG配置
  4. java cmos_CMOS CCD
  5. matlab光学仿真程序,高等光学仿真》matlab源程序
  6. 我对于C女士的初次看法
  7. Python GUI库Tkinter的使用
  8. GBase 8c 函数和操作符 - HLL函数和操作符 之 聚合函数
  9. 49深入聚合数据分析_cardinality算法之优化内存开销以及HLL算法
  10. matlab wik,MATLAB - Calculus