转载地址:http://www.cnblogs.com/sword-successful/p/4031823.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
        }
    }
  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. /// <summary>
  25. /// Office文件转pdf文件
  26. /// </summary>
  27. /// <param name="officePath">office文件保存路径</param>
  28. /// <param name="officeFileName">office文件名</param>
  29. /// <param name="pdfPath">保存pdf路径</param>
  30. protected string OfficeToPdf(string officePath, string officeFileName, string pdfPath)
  31. {
  32. string fullPathName = Path.Combine(officePath, officeFileName);
  33. string fileNameWithoutEx = Path.GetFileNameWithoutExtension(officeFileName);
  34. string ext = Path.GetExtension(officeFileName).ToLower();
  35.  
  36. string savePdfPath = pdfPath + fileNameWithoutEx + ".pdf";
  37. string retValue = fileNameWithoutEx + ".pdf";
  38.  
  39. switch (ext)
  40. {
  41. case ".doc":
  42. OfficeHelper.WordToPdf(fullPathName, savePdfPath);
  43. break;
  44. case ".docx":
  45. OfficeHelper.WordToPdf(fullPathName, savePdfPath);
  46. break;
  47. case ".xls":
  48. OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
  49. break;
  50. case ".xlsx":
  51. OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
  52. break;
  53. case ".ppt":
  54. OfficeHelper.PptToPdf(fullPathName, savePdfPath);
  55. break;
  56. case ".pptx":
  57. OfficeHelper.PptToPdf(fullPathName, savePdfPath);
  58. break;
  59. }
  60.  
  61. return retValue;
  62. }

参考

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

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

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

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

在线预览Office文件【效果类似百度文库】(转载)的更多相关文章

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

    引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好. 提到Offic ...

  2. 在线预览office文件

    Office Online 实现在线预览 office的在线预览,针对不同的浏览器版本和系统具有要求,具体的相关文档请参考官方文档. 利用office online 平台进行office 文档的在线查 ...

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

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

  4. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  5. 用pdf.js实现在移动端在线预览pdf文件

    用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js    官网地址:https://mozilla.github.io/pdf.js/ 2.配置    下载下来的文件包,就是一个demo ...

  6. 使用pdfjs插件在线预览PDF文件

    前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...

  7. asp.net在线预览txt文件(简单实现)

    最近在做文件的在线预览,发现txt文件没有一个较好的方法去实现,想了想可能是比较简单就直接在后台输出了 txt文件

  8. WinForm中预览Office文件

    WinForm预览Office文档 使用WinForm, WPF, Office组件 原理:使用Office COM组件将Word,Excel转换为XPS文档, 将WPF的DocumentViewer ...

  9. linux在线预览pdf文件开发思路

    准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...

随机推荐

  1. OpenCV图像Canny边缘检测

    Canny边缘检测 图像的边缘检测的原理是检测出图像中所有灰度值变化较大的点,而且这些点连接起来就构成了若干线条,这些线条就可以称为图像的边缘函数原型:     void cvCanny(       ...

  2. swing复制文本框内容

    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //得到系统剪贴板 String text = jTex ...

  3. postgresql数据库实用操作

    查模型的列名: select column_name from information_schema.columns  where table_name= 'your_table'; 应用: 1. 给 ...

  4. uniq命令注意事项,检查重复行的时候,只会检查相邻的行。

    今天在使用uniq命令统计数量时,uniq -c总是得不到想要的效果,相同的行没有合并,例如 后来在http://ju.outofmemory.cn/entry/78365才看到,原来uniq检查重复 ...

  5. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  6. centos7.0 安装LNMP运行环境

    LNMP作为php流行的运行环境,而最近需要搭建一个内部的php论坛.记录下LNMP的安装: 1.安装mysql 请参考:centos7 安装mysql5.7.11注意事项 2.安装php yum i ...

  7. MongoDB 副本集管理(不定时更新)

    简介: 前面介绍完了副本集的搭建.用户的管理.参数和日常操作的说明,那副本集搭建好该如何管理呢?现在来说明下副本集的日常查看和管理. 说明: 1)查看命令行参数:db.serverCmdLineOpt ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. 【XLL API 函数】xlGetName

    以字符串格式返回 DLL 文件的长文件名. 原型 Excel12(xlGetName, LPXLOPER12 pxRes, 0); 参数 这个函数没有参数 属性值和返回值 返回文件名和路径 实例 \S ...

  10. Hibernate双向多对多对象关系模型映射

    1 双向many-to-many 业务模型: 描述员工和项目 一个员工同时可以参与多个项目 一个项目中可以包含多个员工 分析:数据库的数据模型,通过中间关系表,建立两个one-to-many构成man ...