在線預覽Office文件【效果相似百度文庫】(轉載)

轉載:http://www.cnblogs.com/yxlblogs/p/4139167.htmlhtml

引言 

       結合上個項目和目前作的這個項目,其中都用到了Office文件在線預覽,目前項目中是用到公司購買的Ntko控件,該控件每次瀏覽 文件時則會提示安裝信任插件,很繁瑣,並且瀏覽效果很差。 提到Office文件在線預覽,那麼效果最好的應該就是百度文庫的效果了,因此今天就忙裏偷閒 本身搞了下。app

 

用到知識點

   一、Office文件轉化爲Pdf文件。直接用.Net類庫:Microsoft.Office.Interop.Excel、 Microsoft.Office.Interop.Powerpoint、Microsoft.Office.Interop.Word、 Office。 我本機裝的office2013,因此我選擇的是12.0的。工具

   二、使用SwfTools將Pdf文件轉化爲Swf文件。學習

   三、使用衆所周知的FlexPaper瀏覽Swf文件(預覽時有水印,不知道怎麼去掉)。ui

 

Demo過程當中遇到的問題

   一、提示:"沒法嵌入互操做類型Microsoft.Office.Interop.Word.ApplicationClass,請改用使用的接口"spa

        解決:右鍵Dll,嵌入互操做類型改成false便可。插件

   二、用到MsoTriState.msoTrue枚舉類型參數時須要飲用Office.dll。 我一開始就沒引用這個文件。code

   三、生成Swf文件時須要傳入完整的路徑,我一開始只傳入了路徑,沒有swf文件名,試了幾回沒成功。orm

效果圖

轉化代碼

  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的過程當中,學習和參考了兩位博友的文章,在此表示感謝htm

      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

相關文章
相關標籤/搜索