引用自:https://bbs.csdn.net/topics/391937345
1.在项目中添加引用 Microsoft PowerPoint 14.0 Object Library
2. using Microsoft.Office.Interop.PowerPoint;
3.创建一个PPT,添加一个空白页

Microsoft.Office.Interop.PowerPoint.Application PPT = new Microsoft.Office.Interop.PowerPoint.Application();//创建PPT应用
Microsoft.Office.Interop.PowerPoint.Presentation MyPres = null;//PPT应用的实例
Microsoft.Office.Interop.PowerPoint.Slide MySlide = null;//PPT中的幻灯片

MyPres = PPT.Presentations.Open("文件路径", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);//此处将一个PPT实例给了MyPres

MySlide = MyPres.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);//像PPT实例中,添加一个空白页,位置是“第一页”

4.向PPT的幻灯片中添加元素

4.1文本框

Microsoft.Office.Interop.PowerPoint.TextRange MyTextRng = null;

MySlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 21.5F, 365F, 670F, 270F);

MyTextRng = MySlide.Shapes[1].TextFrame.TextRange;//请注意此处Shapes的索引,由于文本框是第一个添加的Shapes,所以此处索引是1。

MyTextRng.Font.NameFarEast = "微软雅黑";//文本框中,中文的字体                   
MyTextRng.Font.NameAscii = "Calibri";//文本框中,英文和数字的字体      
MyTextRng.Text ="C#生成PPT";//显示的内容
MyTextRng.Font.Bold = MsoTriState.msoTrue;//是否加粗
MyTextRng.Font.Color.RGB = A+ B * 256 + C * 256 * 256;//字体颜色,其中ABC直接用自定义颜色中的数字代替即可。
MyTextRng.Characters(1, 10).Font.Size = 24;//个性化设计。第1个字符开始,长度为10的字符,字体大小是24.
MyTextRng.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;//文本对齐方式(水平方向)
MySlide.Shapes[1].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; 文本对齐方式(垂直方向)

心得:最重要的设置在Font属性中。其他设置,基本可以参考PPT中元素属性的设置方式来找到。比如我在写文本水平对齐方式时(左对齐,居中,右对齐),在PPT中,我们直接点击“段落”中的快捷键即可。所以我就找“段落”的英文,正好Alignment是对齐的意思,所以就找到了。

4.2 图形(矩形)

MySlide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 8.5F, 6.5F, 705F, 525F);

MySlide.Shapes[1].Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;//改变线条颜色
MySlide.Shapes[1].Fill.Transparency = 1;//控制填充色为透明
MySlide.Shapes[1].Line.Style = MsoLineStyle.msoLineSingle;//改变线型里的复合类型
MySlide.Shapes[1].Line.Weight = 1F;//改变线粗细
MySlide.Shapes[1].Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制阴影类型
MySlide.Shapes[1].Shadow.ForeColor.RGB = 0;//控制阴影颜色
MySlide.Shapes[1].Shadow.Transparency = 0.6F;//控制透明度
MySlide.Shapes[1].Shadow.Size = 100F;//控制大小
MySlide.Shapes[1].Shadow.Blur = 4F;//控制虚化
MySlide.Shapes[1].Shadow.OffsetX = 2.1F;//控制距离;
MySlide.Shapes[1].Shadow.OffsetY = 2.1F;//与offsetX共同决定角度

心得:基本的一些设置,通过英文就可以辨别。不过有些属性的设置是否与预期一致,需要等图形生成后再进一步确认。

4.3 图片

MySlide.Shapes.AddPicture("文件路径", MsoTriState.msoFalse, MsoTriState.msoTrue, 27F, 24F, 665F, 333F);

4.4 表格

Microsoft.Office.Interop.PowerPoint.Table MyTable = null;

MyTable = MySlide.Shapes.AddTable(19, 5, 40F, 100F, 10F, 10F).Table;//创建时规定的宽和高,不是表格最终的大小。

MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Size = 10;
MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Color.RGB = A + B * 256 + C * 256 * 256;
MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.NameAscii = "Arial";
MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.NameFarEast = "微软雅黑";
MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;
MyTable.Cell(k, j).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter;
MyTable.Cell(k, j).Shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;
MyTable.Cell(k, j).Shape.Fill.ForeColor.RGB = 0;
MyTable.Cell(k, j).Shape.TextFrame.TextRange.Text = "C#生成PPT";

这里的设置,几乎和文本框的设置一样。只不过需要先选定Cell。一些个性化的设计,比如合并拆分单元格,边框颜色,按照一般的英文意思都能找到。

4.5 图表

Microsoft.Office.Interop.PowerPoint.Chart MyChart = null;//图表
Microsoft.Office.Interop.PowerPoint.ChartData MyChartData = null;//图表的数据源
Microsoft.Office.Interop.PowerPoint.Axis MyYvalaxis = null;//图表的纵坐标
Microsoft.Office.Interop.PowerPoint.Axis MyXvalaxis = null;//图表的横坐标
Microsoft.Office.Interop.PowerPoint.DataLabels MyDataLabels = null;//图表的数据标签
Microsoft.Office.Interop.PowerPoint.Series MySeries = null;//数据系列
Microsoft.Office.Interop.PowerPoint.ChartGroups MyChartGroups = null;//数据系列-系列选项
Microsoft.Office.Interop.PowerPoint.Points MyPoints = null; //数据系列

MyChart = MySlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xlColumnClustered, 35F, 205F, 642F, 227F).Chart;//添加柱形图

MyChartData = MyChart.ChartData;//实例化数据源

Microsoft.Office.Interop.Excel.Workbook MyDataWorkbook_2 = (Microsoft.Office.Interop.Excel.Workbook)MyChartData.Workbook;//由于PPT的数据源是EXCEL工作表,所以此处还要调用EXCEL。

MyDataWorkbook_2.Application.WindowState = XlWindowState.xlMinimized;//不想看那么多窗口,所以最小化了。

Microsoft.Office.Interop.Excel.Worksheet MyDataWorksheet_2 = (Microsoft.Office.Interop.Excel.Worksheet)MyDataWorkbook_2.Worksheets[1];//实例化工作表

Microsoft.Office.Interop.Excel.Range tRange_2 = MyDataWorksheet_2.Cells.get_Range("A1", "C10");//选定数据区域

Microsoft.Office.Interop.Excel.ListObject tbl1_2 = MyDataWorksheet_2.ListObjects[1];
tbl1_2.Resize(tRange_2);

//赋值  
((Microsoft.Office.Interop.Excel.Range)(MyDataWorksheet_2.Cells.get_Range("A2"))).FormulaR1C1 = "全国得分";
((Microsoft.Office.Interop.Excel.Range)(MyDataWorksheet_2.Cells.get_Range("A3"))).FormulaR1C1 = null;
                    
//图表标题
MyChart.ChartTitle.Delete();

//纵轴
MyYvalaxis = (Microsoft.Office.Interop.PowerPoint.Axis)MyChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);

MyYvalaxis.MajorGridlines.Delete();//删除主横网络线
MyYvalaxis.MajorUnit = 0.5F;
MyYvalaxis.MinimumScale = 0.0F;
MyYvalaxis.MaximumScale = 1.5F;
MyYvalaxis.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256; ;//坐标轴颜色
MyYvalaxis.Format.Line.Transparency = 1F;//坐标轴是否透明;此句必须先指定颜色,否则无效              
MyYvalaxis.TickLabels.Delete();//删除坐标标签

//横轴
MyXvalaxis = (Microsoft.Office.Interop.PowerPoint.Axis)MyChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlCategory, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);

MyXvalaxis.MajorTickMark = Microsoft.Office.Interop.PowerPoint.XlTickMark.xlTickMarkOutside;//主要刻度线类型
MyXvalaxis.Format.Line.Weight = 0.75F;//线型宽度
MyXvalaxis.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;//线条颜色
MyXvalaxis.TickLabelPosition = Microsoft.Office.Interop.PowerPoint.XlTickLabelPosition.xlTickLabelPositionNone;

//图例
MyChart.Legend.Delete();

//数据标签格式和系列
//系列1
MySeries = (Microsoft.Office.Interop.PowerPoint.Series)MyChart.SeriesCollection(1);
MySeries.HasDataLabels = true;
MySeries.Format.Fill.ForeColor.RGB = A + B * 256 + C * 256 * 256;
MySeries.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;
MySeries.Format.Line.Weight = 1.5F;

MySeries.Format.Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制阴影类型
MySeries.Format.Shadow.ForeColor.RGB = 0;//控制阴影颜色
MySeries.Format.Shadow.Transparency = 0.6F;//控制透明度
MySeries.Format.Shadow.Size = 100F;//控制大小
MySeries.Format.Shadow.Blur = 4F;//控制虚化
MySeries.Format.Shadow.OffsetX = 2.1F;//控制距离; 
MySeries.Format.Shadow.OffsetY = 2.1F;//与offsetX共同决定角度

//柱子颜色
MyPoints = (Microsoft.Office.Interop.PowerPoint.Points)MySeries.Points();

MyPoints.Item(1).Format.Fill.ForeColor.RGB = A + B * 256 + B * 256 * 256;//系列1中,第1个柱子的颜色

//柱子距离
MyChartGroups = (Microsoft.Office.Interop.PowerPoint.ChartGroups)MyChart.ChartGroups();
MyChartGroups.Item(1).GapWidth = 50;

//数据标签
MyDataLabels = (Microsoft.Office.Interop.PowerPoint.DataLabels)MySeries.DataLabels();
MyDataLabels.Position = Microsoft.Office.Interop.PowerPoint.XlDataLabelPosition.xlLabelPositionOutsideEnd;
MyDataLabels.NumberFormat = "0.0%";
MyDataLabels.Format.TextFrame2.TextRange.Font.Size = 9F;
MyDataLabels.Format.TextFrame2.TextRange.Font.NameAscii = "Calibri";
MyDataLabels.Format.TextFrame2.TextRange.Font.Bold = MsoTriState.msoTrue;

//系列2
MySeries = (Microsoft.Office.Interop.PowerPoint.Series)MyChart.SeriesCollection(2);
MySeries.HasDataLabels = true;
MySeries.Format.Fill.ForeColor.RGB = A + B * 256 + C * 256 * 256;
MySeries.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;
MySeries.Format.Line.Weight = 1.5F;

MySeries.Format.Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制阴影类型
MySeries.Format.Shadow.ForeColor.RGB = 0;//控制阴影颜色
MySeries.Format.Shadow.Transparency = 0.6F;//控制透明度
MySeries.Format.Shadow.Size = 100F;//控制大小
MySeries.Format.Shadow.Blur = 4F;//控制虚化
MySeries.Format.Shadow.OffsetX = 2.1F;//控制距离
MySeries.Format.Shadow.OffsetY = 2.1F;//与offsetX共同决定角度

//柱子距离
MyChartGroups = (Microsoft.Office.Interop.PowerPoint.ChartGroups)MyChart.ChartGroups();
MyChartGroups.Item(1).GapWidth = 50;

//数据标签
MyDataLabels = (Microsoft.Office.Interop.PowerPoint.DataLabels)MySeries.DataLabels();
MyDataLabels.Position = Microsoft.Office.Interop.PowerPoint.XlDataLabelPosition.xlLabelPositionOutsideEnd;
MyDataLabels.NumberFormat = "0.0%";
MyDataLabels.Format.TextFrame2.TextRange.Font.Size = 9F;
MyDataLabels.Format.TextFrame2.TextRange.Font.NameAscii = "Calibri";
MyDataLabels.Format.TextFrame2.TextRange.Font.Italic = MsoTriState.msoTrue;

c#生成PPT总结(Microsoft.Office.Interop)相关推荐

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

    文章目录 前言 一.步骤 1.引入库 2.创建PPT 3.创建PPT中的幻灯片 4.生成标题 5.生成目录 5.创建表格 6.保存 总结 前言 开发的一个系统需要用到自动生成ppt,网上的例子比较少, ...

  2. C# 用Microsoft.Office.Interop.PowerPoint类库操作PPT

    前言:最近由于项目需求,需要使用此类库对PPT进行操作 1.引用 Microsoft.Office.Interop.PowerPoint和 Microsoft.Office.Core 2.PPT操作 ...

  3. 使用Microsoft.Office.Interop.PowerPoin遥控PPT

    Microsoft.Office.Interop.PowerPoin操作PPT 主窗体,填写ppt路径,打开ppt 打开ppt后,可用代码操作ppt 可获取每页PPT截图,并获取对应小节名称,备注等 ...

  4. Net使用Microsoft.Office.Interop.Excel;创建Excel文件(插入数据、修改格式、生成图表)的方法,以及Excel查看加密

    具体使用方法可以参照文章: http://blog.csdn.net/tuoxie5431/article/details/3937752 Microsoft.Office.Interop.Excel ...

  5. C# 使用Microsoft.Office.Interop将Excel、Word转换成PDF遇到的问题总结

    首先应用中引入Microsoft.Office.Interop.Excel.Microsoft.Office.Interop.Word两个dll,将嵌入式互操作类型设为False, WORD转换成PD ...

  6. window2008 64位系统无法调用Microsoft.Office.Interop组件进行文件另存的解决办法

    服务器是windows server2008 64位系统,项目中需要用到Microsoft.Office.Interop组件,包括excel.word.ppt等. 步骤  1.在"开始&qu ...

  7. 无法引用Microsoft.Office.Interop.Excel的解决

    微软MSDN文章: http://msdn.microsoft.com/zh-cn/library/aa159923(office.11).aspx 下载 在 Microsoft Office 200 ...

  8. ” Microsoft.Office.Interop.Excel”无法引用

    首先是添加了Microsoft Excel11 Object Libray之后,发现引用里面的" Microsoft.Office.Interop.Excel"不能用.出现了一个可 ...

  9. c# 使用Microsoft.Office.Interop.Excel 对Excel操作

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.pandas是什么? 二.使用步骤 1.引入库 2.读入数据 总结 前言 Microsoft.Office.Int ...

最新文章

  1. Oracle数据库迁移-基础
  2. 报名开启!AI+科学计算专场直播:大咖齐聚×独家礼物,限量200份
  3. c++ 类的继承与派生
  4. ffmpeg 源码学习之seek play
  5. P3308-[SDOI2014]LIS【最小割】
  6. datatype未定义是什么意思_vue.js一直提示未定义
  7. Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果
  8. lr LoadRunner Internal Architecture loadrunner运行原理图解
  9. 032——VUE中表单控件处理之复选框的处理
  10. Neo4j从mysql读取数据_[bigdata-086] python3+neo4j 从mysql数据库读取记录然后创建节点和关系写入到neo4j...
  11. 海王什么意思,海王是什么意思梗,网络流行词海王介绍
  12. Maven中如何配置WAR依赖WAR和JAR的多模块项目结构
  13. 代码整洁 vs 代码肮脏
  14. SpringBoot+SQLSERVER2000问题 简要总结
  15. 中美联合挫败Conficker蠕虫大攻击
  16. 从计算机视觉到人脸识别:一文看懂颜色模型、信号与噪声
  17. Ubuntu/Debian怎么在命令行开启远程桌面共享
  18. Nginx--流量限制(最有用的功能之一)
  19. 友元介绍以及实例说明
  20. 基于Android的文本语音朗读器的设计与实现(有声小说APP)

热门文章

  1. 基于ESP8266的STM32物联网开发板
  2. 2021知识付费、流量变现小程序源码系统搭建安装教程,一个小白都可以日入过千的项目。
  3. 【扩张卷积or空洞卷积】如何理解Dilated Convolutions(空洞卷积)
  4. 智慧养老模式和智慧养老系统
  5. C++11 的 运行时类型识别type_info
  6. 关不掉,新版微信这功能引用户怨声载道...
  7. Windows Update错误80070003解决方法
  8. 计算机一级ps考级知识,Photoshop等级考试一级大纲
  9. 使用阿里云IoT实现远程windows远程桌面
  10. 解密excel工作表打开密码