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

转载: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文件名,试了几次没成功。

效果图

在线预览Office文件【效果类似百度文库】(转载)_第1张图片

转化代码

  1 public class OfficeHelper
  2     {
  3         /// 
  4         /// Word to Pdf
  5         /// 
  6         /// 
  7         /// 
  8         /// 
  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         /// 
 54         /// Excel To Pdf
 55         /// 
 56         /// 
 57         /// 
 58         /// 
 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         /// 
104         /// PPT To Pdf
105         /// 
106         /// 
107         /// 
108         /// 
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         /// 
151         /// Pdf To Swf
152         /// 
153         /// Swf转化工具路径
154         /// 
155         /// 
156         /// 
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 /// 
 2         /// Pdf文件转化为Swf
 3         /// 
 4         /// 转化工具路径
 5         /// pdf文件目录
 6         /// pdf文件名
 7         /// 保存swf路径
 8         /// 
 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 /// 
26         /// Office文件转pdf文件
27         /// 
28         /// office文件保存路径
29         /// office文件名
30         /// 保存pdf路径
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://www.cnblogs.com/jbps/p/4505304.html

你可能感兴趣的:(在线预览Office文件【效果类似百度文库】(转载))