前言 特點 成熟,穩定 消息持久化 靈活的消息路由 高性能,高可用性,可擴展性高 支持插件系統:RabbitMQ 具有豐富的插件系統,可以通過安裝插件來擴展其功能,例如管理界面、消息追蹤、消息轉換等。 ...
上一篇我們使用的是微軟的Office組件將Word、Excel、Powerpoint轉為pdf格式,本文將使用WPS Office組件進行轉換。步驟如下:
① 添加WPS組件相關引用
註:wpsapi.dll 對應的是Word 文件API;etapi.dll 對應的是Excel 文件API;wppapi 對應的是PPT 文件API;
② 編寫Office幫助類
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class WPSOfficeHelper { /// <summary> /// Word轉換為pdf文件,適合(.doc、.docx、.mht、.htm文件類型) /// </summary> /// <param name="sourceFileName">源文件</param> /// <param name="targetFileName">目標文件</param> /// <returns></returns> public static bool WordToPdf(string sourceFileName, string targetFileName) { Word.Application wordApp = new Word.Application(); Word._Document wordDoc = null; try { wordApp.Visible = false; wordDoc = wordApp.Documents.Open(sourceFileName, false, true); wordDoc.ExportAsFixedFormat(targetFileName, Word.WdExportFormat.wdExportFormatPDF); return true; } catch (Exception ex) { return false; } finally { if (wordDoc != null) { wordDoc.Close(false); wordDoc = null; } if (wordApp != null) { wordApp.Quit(false); wordApp = null; } } } /// <summary> /// Excel轉換為pdf文件 /// </summary> /// <param name="sourceFileName">源文件</param> /// <param name="targetFileName">目標文件</param> /// <returns></returns> public static bool ExcelToPdf(string sourceFileName,string targetFileName) { Excel.Application excelApp = new Excel.Application(); Excel._Workbook excelDoc = null; try { excelApp.Visible = false; excelDoc = excelApp.Workbooks.Open(sourceFileName, false, true); excelDoc.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, targetFileName); return true; } catch (Exception ex) { return false; } finally { if (excelDoc != null) { excelDoc.Close(false); excelDoc = null; } if (excelApp != null) { excelApp.Quit(); excelApp = null; } } } /// <summary> /// PPT轉換為pdf文件 /// </summary> /// <param name="sourceFileName">源文件</param> /// <param name="targetFileName">目標文件</param> /// <returns></returns> public static bool PPTToPdf(string sourceFileName, string targetFileName) { PowerPoint.Application pptApp = new PowerPoint.Application(); PowerPoint.Presentation pptDoc = null; try { pptDoc = pptApp.Presentations.Open(sourceFileName); pptDoc.ExportAsFixedFormat(targetFileName,PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF); return true; } catch (Exception ex) { return false; } finally { if (pptDoc != null) { pptDoc.Close(); pptDoc = null; } if (pptApp != null) { pptApp.Quit(); pptApp = null; } } } }View Code
最後,就可以調用進行轉換了。
註意:
①該方式目前只能用於Windows系統
②該方式依賴WPS Office軟體
③在.net framework和.net core的項目下均可使用(以Win Form項目為例)