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是增加的页码
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组件权限的方法就自行百度吧,有很多介绍的,希望能帮到大家。

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C#使用 Microsoft Office 的 Excel 应用程序的编程接口,可以通过以下步骤生成折线图: 1. 创建 Excel 应用程序对象和工作簿对象,并打开 Excel 文件: ``` var excelApp = new Microsoft.Office.Interop.Excel.Application(); var workBook = excelApp.Workbooks.Open("path/ExcelFile.xlsx"); ``` 2. 获取工作表对象,并选择需要生成折线图的区域: ``` var workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1]; var range = workSheet.Range["A1:B10"]; ``` 3. 创建一个新的图表对象: ``` var charts = (Microsoft.Office.Interop.Excel.ChartObjects)workSheet.ChartObjects(Type.Missing); var chartObject = charts.Add(100, 100, 300, 300); var chart = chartObject.Chart; ``` 4. 设置图表类型为折线图: ``` chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine; ``` 5. 将数据源设置为选择的区域: ``` var seriesCollection = (Microsoft.Office.Interop.Excel.SeriesCollection)chart.SeriesCollection(Type.Missing); var series = seriesCollection.NewSeries(); series.Values = range; ``` 6. 格式化图表,例如设置坐标轴标签和标题等: ``` chart.HasTitle = true; chart.ChartTitle.Text = "折线图"; var axis = (Microsoft.Office.Interop.Excel.Axis)chart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary); axis.HasTitle = true; axis.AxisTitle.Text = "X轴"; axis = (Microsoft.Office.Interop.Excel.Axis)chart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary); axis.HasTitle = true; axis.AxisTitle.Text = "Y轴"; ``` 7. 关闭 Excel 应用程序,并释放相关资源: ``` workBook.Close(false); System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelApp.Quit(); ``` 需要注意的是,在使用完 Excel 应用程序对象和工作簿对象后,需要调用 `System.Runtime.InteropServices.Marshal.ReleaseComObject()` 方法来释放相关资源,否则可能会导致内存泄漏。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值