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

这篇具有很好参考价值的文章主要介绍了C#使用Microsoft.office.interop.PowerPoint生成PPT。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

开发的一个系统需要用到自动生成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是增加的页码

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组件权限的方法就自行百度吧,有很多介绍的,希望能帮到大家。文章来源地址https://www.toymoban.com/news/detail-508150.html

到了这里,关于C#使用Microsoft.office.interop.PowerPoint生成PPT的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# WinForm 使用Microsoft.Office.Interop.Excel对Excel文件表格的单元格值进行修改操作

    在引用位置点击右键 点击管理NUGet程序包 搜索Excel,在搜索结果中点击Microsoft.Office.Interop.Excel 然后点击安装即可,搜不到的话,在右侧程序包源位置改成全部再次搜索 。 对准项目,点击右键,在弹出菜单选择添加,选择类 类名称随意,然后清空新建类得到全部内容,把下面

    2024年02月13日
    浏览(41)
  • C#调用office interop接口打开word、excel、ppt,拦截处理关闭、保存事件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WordLib = Microsoft.Office.Interop.Word; using ExcelLib = Microsoft.Office.Interop.Excel; using PptLib = Microsoft.Office.Interop.PowerPoint

    2024年02月07日
    浏览(68)
  • C#调用Microsoft.Office.Interop.Excel时的常见操作功能

    using Microsoft.Office.Interop.Excel;//引用Microsoft.Office.Interop.Excel //声明一个Excel Application  Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application();                 //声明一个Excel Workbook                 Microsoft.Office.Interop.Excel.Workbook workbookData;            

    2024年02月12日
    浏览(36)
  • Asp.net Core使用Microsoft.Office.Interop.Word转换文档

    源代码网络上有很多,但是使用过程中发现问题还是有的,尤其是在文档属性配置不同的时候,出现的问题就是不能转换。在开发端没有问题,但在不同于开发端电脑的配置情况下,会出现意向不到的问题。此处对解决方案进行记录。 using Word = Microsoft.Office.Interop.Word;   pub

    2024年02月05日
    浏览(49)
  • 关于使用Microsoft.Office.Interop.Excel.dll来读写Excel表格的问题

    小公司,使用打卡机进行考勤,输出的是一张Excel表格,每个月看到前台妹子都要辛辛苦苦地统计Excel表格每个员工迟到的时间,于是想着写一个自动计算迟到分钟的小程序。 参照这几篇比较好的文章按部就班地做:导出Excel文件 、其它方法。 没想到刚开始写就遇到一个大坑

    2024年02月16日
    浏览(34)
  • Microsoft Office( Word、Excel、PowerPoint)的安装

    官网地址:点击下载

    2024年02月08日
    浏览(81)
  • Office技巧(持续更新)(Word、Excel、PPT、PowerPoint、连续引用、标题、模板、论文)

    选住 一级标题 ,之后进行“定义新的多级列表”    正常插入题注后就可以了。如果一级标题是 “汉字序号”,那么需要对题注进行修改: 从原来的 图 { STYLEREF 1 s }-{ SEQ 图 * ARABIC s 1 } 修改为 图 { Quote “二零二五年一月{ STYLEREF 1 s }日” @”d” }-{ SEQ 图 * ARABIC s 1 } 注

    2024年02月08日
    浏览(46)
  • microsoft.office.interop.word 怎样 读取 某个汉字 字体颜色为红色

    SKY[管理]筱傑 @SKY[机器]筱淋 microsoft.office.interop.word 怎样 读取 某个汉字 字体颜色为红色呢? 要读取某个汉字的字体颜色是否为红色,您可以使用Microsoft.Office.Interop.Word来进行操作。以下是一个示例代码,可以帮助您实现该功能: 请注意,您需要替换\\\"your_document_path.docx\\\"为您要

    2024年02月09日
    浏览(35)
  • Microsoft.Office.Interop.Word的COM 对象强制转换为接口类型失败

    System.InvalidCastException:“无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此操作失败的原因是对 IID 为“{00020970-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库

    2024年02月12日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包