引言
結合上個項目和目前做的這個項目,其中都用到了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文件名,試了幾次沒成功。
效果圖
轉化代碼
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
源代碼:http://yunpan.cn/csSPiPeVuum4s (提取碼:4c7c)