前言:最近由于项目需求,需要将ppt的背景色和字体色统一设置成一种颜色(详细需求不再赘述)、PPT转图片,通过这几天的查资料和怼微软的API总结如下。本文主要总结C#使用office组件操作office全家桶。
1.操作PPT(设置PPT的背景色为白色【需要注意渐进色】,字体色为黑色)
(1)需要添加的引用:

(2)代码 - 代码中的注解很详细,不再赘述
Microsoft.Office.Interop.PowerPoint.Application pptApp;
//PPT应用程序变量
Microsoft.Office.Interop.PowerPoint.Presentation pptDoc;
//PPT文档变量
pptApp =
new
Microsoft.Office.Interop.PowerPoint.Application();
//初始化
pptDoc = pptApp.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
//打开PPT
int
size = pptDoc.Slides.Count;
//ppt的幻灯片数量
int
i = 0;
foreach
(Microsoft.Office.Interop.PowerPoint.Slide slide
in
pptDoc.Slides)
{
slide.FollowMasterBackground = MsoTriState.msoFalse;
//不采用模板的背景色
slide.Background.BlackWhiteMode = MsoBlackWhiteMode.msoBlackWhiteAutomatic;
//设置背景色为黑白模式
slide.Background.Fill.OneColorGradient(MsoGradientStyle.msoGradientHorizontal, 1, 1.0f);
//去掉渐进色(只采用黑白模式无法去除渐进色)
slide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(255, 255, 255));
//设置前景色为白色
slide.Background.Fill.BackColor.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(255, 255, 255));
//设置背景色为白色
//遍历每个幻灯片的方块
foreach
(Microsoft.Office.Interop.PowerPoint.Shape shape
in
slide.Shapes)
{
shape.BlackWhiteMode = MsoBlackWhiteMode.msoBlackWhiteAutomatic;
if
(shape.TextFrame.HasText == MsoTriState.msoTrue)
//判断方块是否有字体,如果没有不设置,不然报异常
{
if
(shape.TextFrame.TextRange !=
null
)
{
if
(shape.TextFrame.TextRange.Text !=
null
)
{
if
(shape.TextFrame.TextRange.Text !=
""
)
{
shape.TextFrame.TextRange.Font.Color.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(0, 0, 0));
//设置字体为黑色
}
}
}
}
}
}
//WdSaveFormat为PPT文档的保存格式
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType format = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault;
//保存(另存为)
pptDoc.SaveAs(pptSavePath, format, Microsoft.Office.Core.MsoTriState.msoFalse);
//关闭excelDoc文档对象
pptDoc.Close();
//关闭excelApp组件对象
pptApp.Quit();
2.PPT转图片
string
imgPath =
""
;
int
width = 0;
int
height = 0;
String imgType =
"png"
;
String baseName =
"test"
;
Microsoft.Office.Interop.PowerPoint.Application pptApp;
//PPT应用程序变量
Microsoft.Office.Interop.PowerPoint.Presentation pptDoc;
//PPT文档变量
pptApp =
new
Microsoft.Office.Interop.PowerPoint.Application();
//初始化
pptDoc = pptApp.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
int size = pptDoc.Slides.Count;
int
i = 0;
foreach
(Microsoft.Office.Interop.PowerPoint.Slide slide
in
pptDoc.Slides)
{
String outName = String.Format(
@"{0}{1}_slide{2}.{3}"
, imgPath, baseName, i++, imgType);
try
{
slide.Export(outName, imgType, width, height);//导出图片
}
catch
(Exception ex)
{
}
}
//WdSaveFormat为PPT文档的保存格式
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType format = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault;
//保存
pptDoc.SaveAs(pptSavePath, format, Microsoft.Office.Core.MsoTriState.msoFalse);
//关闭excelDoc文档对象
pptDoc.Close();
//关闭excelApp组件对象
pptApp.Quit();