TerraMoursGPT V1.0 開發總結 TerraMoursGPT V1.0 是之前gpt項目基於TerraMours後端框架的重構,實現用戶登陸和基於SK的多語言模型聊天、基於chatgpt和SD的多模型圖片生成等功能。管理端實現數據看板、聊天記錄管理,圖片記錄管理、用戶管理、系統配置等。 ...
將Office文檔(Word、Excel、Powerpoint)轉為pdf格式,有多種實現方式,最常見的就是使用微軟的Office組件。步驟如下:
① 打開NuGet包管理器,引用以下四個組件:
MicrosoftOfficeCore
Microsoft.Office.Interop.Word
Microsoft.Office.Interop.Excel
Microsoft.Office.Interop.PowerPoint
② 編寫Office幫助類
public static class OfficeHelper { /// <summary> /// Word轉換為PDF /// </summary> /// <param name="sourcePath">源文件路徑</param> /// <param name="targetPath">目標文件路徑</param> /// <param name="exportFormat">導出文件格式</param> /// <returns></returns> public static bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat) { bool result; object paramMissing = Type.Missing; Word.ApplicationClass wordApplication = new Word.ApplicationClass(); Word.Document wordDocument = new Word.Document(); try { object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Word.WdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint; Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); result = true; } finally { if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); } if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } /// <summary> /// Excel轉換為PDF /// </summary> /// <param name="sourcePath">源文件路徑</param> /// <param name="targetPath">目標文件路徑</param> /// <param name="exportFormat">導出文件格式</param> /// <returns></returns> public static bool Convert(string sourcePath, string targetPath, Excel.XlFixedFormatType targetType) { bool result; object missing = Type.Missing; Excel.ApplicationClass application = new Excel.ApplicationClass(); Excel.Workbook workBook = new Excel.Workbook(); try { application = new Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); } if (application != null) { application.Quit(); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } /// <summary> /// PPT轉換為PDF /// </summary> /// <param name="sourcePath">源文件路徑</param> /// <param name="targetPath">目標文件路徑</param> /// <param name="exportFormat">導出文件格式</param> /// <returns></returns> public static bool Convert(string sourcePath, string targetPath, PowerPoint.PpSaveAsFileType targetFileType) { bool result; object missing = Type.Missing; PowerPoint.ApplicationClass application = new PowerPoint.ApplicationClass(); PowerPoint.Presentation persentation = null; try { application = new PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue); result = true; } catch { result = false; } finally { if (persentation != null) { persentation.Close(); } if (application != null) { application.Quit(); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } }View Code
最後,就可以調用進行轉換了。
註意:
①該方式目前只能用於Windows系統
②該方式依賴Microsoft Office軟體
③在.net framework和.net core的項目下均可使用(以Win Form項目為例)