前提
安装office 2019
安装vs 2022
新建项目
引入4个com包
编写代码
代码结构
代码如下
cs
using Microsoft.Office.Interop.Excel;
namespace UseMsOffice
{
internal class Program
{
static void Main(string[] args)
{
WordToPdf();
ExcelToPdf();
PPTToPdf();
}
static void WordToPdf()
{
// Word应用程序对象
var wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document? doc = null;
string root = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase ?? "";
try
{
// 设置为不可见模式
wordApp.Visible = false;
var docPath = Path.Combine(root, "Document", "office.docx");
// 打开一个现有的Word文档
doc = wordApp.Documents.Open(docPath);
// 将文档另存为PDF
string pdfPath = Path.Combine(root, $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_office.pdf");
doc.ExportAsFixedFormat(pdfPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
// 关闭文档和Word应用
doc.Close();
wordApp.Quit();
Console.WriteLine("文档已成功转换为PDF格式。");
}
catch (Exception ex)
{
Console.WriteLine("发生错误:" + ex.Message);
}
finally
{
// 释放COM对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
doc = null;
wordApp = null;
}
}
static void ExcelToPdf()
{
// Excel应用程序对象
var excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = null;
string root = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase ?? "";
try
{
// 设置为不可见模式
excelApp.Visible = false;
var docPath = Path.Combine(root, "Document", "office.xlsx");
// 打开一个现有的Word文档
workBook = excelApp.Workbooks.Open(docPath);
// 将文档另存为PDF
string pdfPath = Path.Combine(root, $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_office.pdf");
workBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfPath);
// 关闭文档和Word应用
workBook.Close();
excelApp.Quit();
Console.WriteLine("文档已成功转换为PDF格式。");
}
catch (Exception ex)
{
Console.WriteLine("发生错误:" + ex.Message);
}
finally
{
// 释放COM对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
workBook = null;
excelApp = null;
}
}
static void PPTToPdf()
{
// Excel应用程序对象
var pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
string root = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase ?? "";
try
{
// 设置为不可见模式
pptApp.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
var docPath = Path.Combine(root, "Document", "office.pptx");
// 打开一个现有的Word文档
presentation = pptApp.Presentations.Open(docPath);
// 将文档另存为PDF
string pdfPath = Path.Combine(root, $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_office.pdf");
presentation.ExportAsFixedFormat(pdfPath,Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
// 关闭文档和Word应用
presentation.Close();
pptApp.Quit();
Console.WriteLine("文档已成功转换为PDF格式。");
}
catch (Exception ex)
{
Console.WriteLine("发生错误:" + ex.Message);
}
finally
{
// 释放COM对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(presentation);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pptApp);
presentation = null;
pptApp = null;
}
}
}
}
测试
参考
https://blog.csdn.net/z542601362/article/details/45158215
https://www.cnblogs.com/5426z/articles/4865312.html
https://blog.csdn.net/qq_35620884/article/details/141326517
https://blog.csdn.net/lzp1990/article/details/138810685