Logo

C#使用Microsoft.office.interop.PowerPoint生成PPT

photo

2024年06月21日


前言

开发的一个系统需要用到自动生成ppt,网上的例子比较少,且有很多还需要csdn币下载,有些下载下来了还不是那么回事,就是个坑。后来考虑了开源组件Apose.Slides和Spire.Presentation,但是这两个都是商业软件,试用版和免费版的只有能生成前10页,还带有水印。Free Spire.Presentation可以没有水印,如果你的需求是不超过十页的,那么你就可以用这个组件。由于我开发的是.Net项目,最终还是选择了微软自带的Microsoft.office.interop.PowerPoint组件,但是它的使用需要在服务器端安装PowerPoint,且需要配置DCOM组件权限。


一、步骤

1.引入库

首先需要引入Microsoft.office.interop.PowerPoint.dll组件。

using Microsoft.office.interop.PowerPoint;

2.创建PPT

Application PPT = new Application();//创建PPT应
Presentation presentation = null;//PPT应用实例
presentation = PPT.Presentations.Open(sourcepath,MsoTriState.msoFalse,MsoTriState.msoFalse,MsoTriState.msoTrue);//此处打开一个PPT实例赋给pre;sourcepath是绝对路径

3.创建PPT中的幻灯片,自动生成页码

Slide slide = null;//PPT中的幻灯片
slide = presentation.Slides.Add(page,PpSlideLayout.ppLayoutBlank);//添加幻灯片,page是增加的页码
slide.HeadersFooters.SlideNumber.Visible = MsoTriState.msoTrue;//页码自动生效,会自动添加页码

4.生成标题

//添加背景图片
slide.FollowMasterBackground = MsoTriState.m8oFalse;
slide.Background Fi11.UserPicture(Server.MapPath(strRootDir + ppt.BackGroundUr1. Replace ("”,“")))://添加背景图片
TextRange textRng =nul1;
slide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientatiorHorizontal ,50,220,800,50);//创建文本框 
textRng = slide.Shapes[1].TextFrame.TextRange;//获取文本框
textRng.Font.NameFarEast =“微软雅黑";//中文字体 
textRng.Font.NameAscii =“微软雅黑”;//数字字体 
textRng.Text = meet.MeetTitle;//文本框内容
textRng.Font.Bold = MsoIriState.moIrue;//字体加粗
textRng.Font.Color.RGB =255+255*256+255*256*256;//字体 RGB 颜色例如:RGB (a ,b ,c ) a + b *256+ c *256*256 
textRng.Font.Size=40;//字体大小
textRng.ParagraphFormat.Alignment = PpParagraphAligrument.ppAlignCenter;//学体居中
slide.Shapes[1].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;//文本框内容垂直居中

5.生成目录

shapeNum = 0;//定义Shape数目
slide.FollowMasterBackground = MsoTriState.msoFalse;                     slide.Background.Fill.UserPicture(Server.MapPath(strRootDir + ppt.BackGroundUrl.Replace("~", "")));//添加背景图片
TextRange textRng = null;//定义文本 
slide.Shapes.AddShape(MsoAutoShapeType.msoShapeRoundedRectangle, 400, 90, 200, 60);//新增圆角矩形框
shapeNum += 1;//Shape数目增加
textRng = slide.Shapes[shapeNum].TextFrame.TextRange;//获取文本框
textRng.Font.NameFarEast = "微软雅黑";//中文字体
textRng.Font.NameAscii = "微软雅黑";//数字字体
textRng.Text = ppt.Title;//文本框内容
textRng.Font.Bold = MsoTriState.msoTrue;//字体加粗
textRng.Font.Color.RGB = 255 + 255 * 256 + 255 * 256 * 256;//字体RGB颜色
textRng.Font.Size = 40;//字体大小
textRng.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;//字体居中
slide.Shapes[shapeNum].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;//圆角矩形框内容垂直居中
slide.Shapes[shapeNum].Fill.ForeColor.RGB = 0 + 112 * 256 + 191 * 256 * 256;//圆角矩形框背景色/填充色

for (int j = 0; j < cataLogList.Count(); j++)
{
   //灵活计算目录标题的高度、字体
   int cataLogSort = j + 1;//目录序号
   int h = 50;//目录标题高度
   float fontSize = 20;//目录字体
   if (cataLogList.Count > 4)
   {
       var str = ((320 / cataLogList.Count) - 10).ToStr();
       h = Int32.Parse(Math.Round(double.Parse(str)).ToStr());
       fontSize = h - 10;
   }
   int y = 90 + 60 + cataLogSort * 15 + j * h;//目录Y轴距离,就是垂直距离
    //序号
    TextRange textRng1 = null;//定义文本
    slide.Shapes.AddShape(MsoAutoShapeType.msoShapeRoundedRectangle, 250, y, 60, h);//新增圆角矩形框
    shapeNum += 1;//Shape数目增加
    textRng1 = slide.Shapes[shapeNum].TextFrame.TextRange;
    textRng1.Font.NameFarEast = "微软雅黑";//中文字体
    textRng1.Font.NameAscii = "微软雅黑";//数字字体
    textRng1.Text = cataLogSort.ToString("00");//文本框内容
    textRng1.Font.Bold = MsoTriState.msoTrue;//字体加粗
    textRng1.Font.Color.RGB = 255 + 255 * 256 + 255 * 256 * 256;//字体RGB颜色
    textRng1.Font.Size = fontSize;//字体大小
    textRng1.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;//字体居中
    slide.Shapes[shapeNum].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;//圆角矩形框内容垂直居中
    slide.Shapes[shapeNum].Fill.ForeColor.RGB = 0 + 112 * 256 + 191 * 256 * 256;//圆角矩形框背景色/填充色

    //主目录
    TextRange textRng2 = null;
    slide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 330, y, 400, h);//新增矩形框
    shapeNum += 1;//Shape数目增加
    textRng2 = slide.Shapes[shapeNum].TextFrame.TextRange;
    textRng2.Font.NameFarEast = "微软雅黑";//中文字体
    textRng2.Font.NameAscii = "微软雅黑";//数字字体
    textRng2.Text = cataLogList[j].name;//文本框内容
    textRng2.Font.Bold = MsoTriState.msoTrue;//字体加粗
    textRng2.Font.Color.RGB = 255 + 255 * 256 + 255 * 256 * 256;//字体RGB颜色
    textRng2.Font.Size = 20;//字体大小
    textRng2.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;//字体居中
    slide.Shapes[shapeNum].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;//矩形框内容垂直居中
    slide.Shapes[shapeNum].Fill.ForeColor.RGB = 0 + 112 * 256 + 191 * 256 * 256;//矩形框背景色/填充色

    dicCataLog.Add(cataLogList[j].CatalogId, cataLogSort.ToString("00"));//目录序列号添加到集合中,便于后续页面用到
}

5.创建表格

nt rowCount = 5;
int colCount = 7;

Table table = slide.Shapes.AddTable(rowCount, colCount, 10, 105, 900, 300).Table;
for (int row = 1; row <= table.Rows.Count; row++)
{
    table.Rows[row].Height = 60;//设置行高
    //下面这几项设置可以不用,看自己需要,我只是列出方法
    //table.Rows[row].Cells.Borders[PpBorderType.ppBorderBottom].ForeColor.RGB= 0 + 74 * 256 + 133 * 256 * 256;//设置边框颜色
    //table.Rows[row].Cells.Borders[PpBorderType.ppBorderLeft].ForeColor.RGB = 0 + 74 * 256 + 133 * 256 * 256;//设置边框颜色
    //table.Cell(row, cell).Borders[PpBorderType.ppBorderTop].Weight = 2.25f;//上边框设置2.25镑
    //table.Cell(row, cell).Borders[PpBorderType.ppBorderLeft].Weight = 2.25f;//左边框
for (int cell = 1; cell <= table.Columns.Count; cell++)
{ 
  table.Columns[cell].Width = 50;//设置列宽
  table.Cell(row, cell).Shape.TextFrame.TextRange.Font.Size = 20;//字体大小
  table.Cell(row, cell).Shape.TextFrame.TextRange.Font.Color.RGB = 0 + 0 * 256 + 0 * 256 * 256;//字体RGB颜色
  table.Cell(row, cell).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter;//居中 (居左:PpParagraphAlignment.ppAlignLeft;  居右:PpParagraphAlignment.ppAlignRight;)
  table.Cell(row, cell).Shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;//垂直居中
  table.Cell(row, cell).Shape.TextFrame.TextRange.Font.NameAscii = "微软雅黑";
  table.Cell(row, cell).Shape.TextFrame.TextRange.Font.NameFarEast = "微软雅黑";
  table.Cell(row, cell).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;//加粗
  table.Cell(row, cell).Shape.TextFrame.TextRange.Text = “AAA”;//单元格赋值
  
  //合并单元格方法
  table.Cell(row, cell).Merge(table.Cell(row, 6));//单元格合并,合并第row行的第cell列到第6列
  table.Cell(row, cell).Shape.TextFrame.TextRange.Text = “”;//单元格赋值
  table.Cell(row, cell).Shape.TextFrame.TextRange.ParagraphFormat.SpaceWithin = 1.5f;//段落间距
  table.Cell(row, cell).Borders[PpBorderType.ppBorderRight].Weight = 2.25f;//右边框设置
  table.Cell(row, cell).Borders[PpBorderType.ppBorderRight].ForeColor.RGB = 0 + 74 * 256 + 133 * 256 * 256;//右边框颜色设置
 }
}

6.保存

presentation.SaveAs(savePath, PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoFalse);//保存PPT到指定路径
presentation.Close();//关闭PPT
PPT.Quit();//关闭PPT应用程序池
GC.Collect();

总结

关于配置DCOM组件权限的方法就自行百度吧,有很多介绍的,希望能帮到大家。

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

Windows Server IIS+ARR反向代理(配置反向代理服务器) 1.概念说明:反向代理反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相...WindowsServerIIS+ARR反向代理(配置反向代理服务器) 作者:Pastore Antonio
1581 浏览量
ffmpeg 生成水印 1:先要配置ffmpeg的滤镜:参考:https://www.jianshu.com/p/9d24...ffmpeg生成水印 作者:Pastore Antonio
1508 浏览量
C#中List的FindAll方法的正确打开方式 初略的介绍一种常见的List写法,这种写法在3.0以后其实是很简单的,但是在2.0左右的系统运用中还...C#中List的FindAll方法的正确打开方式 作者:Pastore Antonio
1467 浏览量
IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 这是在设置IntelliJIDEA...IntelliJIDEA代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 作者:Pastore Antonio
1461 浏览量
Navicat Premium 12.0.22 安装与破解 一、安装  NavicatPremium12.0.22的下载链接:https://pan.ba...NavicatPremium12.0.22安装与破解 作者:Pastore Antonio
1447 浏览量