一、哪些DLL能夠生成PPT
2、Microsoft.Office.Interop.PowerPoint
簡單區別:Spire.Presentation分收費版和免費版(最多創建10張幻燈片)
Microsoft.Office.Interop.PowerPoint是免費的
二、用Spire.Presentation實現創建PPT
1、vs創建控制台應用程序
2、添加引用Spire.Presentation
3、創建Image文件夾放入圖片
4、實現代碼
//創建Presentation對象 Presentation presentation = new Presentation(); //加載圖片作為幻燈片背景 string dir = AppDomain.CurrentDomain.BaseDirectory; dir = Path.GetFullPath("../..") + "\\Image"; string ImageFile = dir + "\\草原.jpg"; RectangleF rect = new RectangleF(0,0,presentation.SlideSize.Size.Width,presentation.SlideSize.Size.Height); presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect); presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite; //獲取第一張幻燈片,添加指定大小的shape到頁面指定位置 IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(260, 220, 400, 295)); //設置shape的邊框顏色 shape.ShapeStyle.LineColor.Color = Color.White; //設置shape填充效果及顏色 shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None; //添加文本到shape,並設置文字填充效果、顏色 shape.AppendTextFrame("重陽節,為每年的農歷九月初九日,是中華民族的傳統節日。" + "《易經》中把“九”定為陽數,九月九日,兩九相重,故曰“重陽”;" + "因日與月皆逢九,故又稱為“重九”。" + "九九歸真,一元肇始,古人認為九九重陽是吉祥的日子。" + "古時民間在重陽節有登高祈福、賞菊、佩茱萸、祭神祭祖及飲宴求壽等習俗。 " + "流傳至今,又添加了敬老等內涵,於重陽之日享宴高會,感恩敬老。"); TextRange textRange = shape.TextFrame.TextRange; textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.Black; //設置文本字號、下划線、字體 textRange.FontHeight = 21; textRange.IsItalic = TriState.False; textRange.TextUnderlineType = TextUnderlineType.Single; textRange.LatinFont = new TextFont("Gulim"); //保存文檔 presentation.SaveToFile("text.pptx", FileFormat.Pptx2010); Console.WriteLine("創建成功!"); Console.ReadKey();
5、效果展示
三、用Microsoft.Office.Interop.PowerPoint實現創建PPT
1、vs創建控制台應用程序
2、添加引用Spire.Presentation
3、添加一個template.pptx空模板
4、實現代碼
public class PowerPointHelper { public static void ReplacePowerPoint() { string dir = AppDomain.CurrentDomain.BaseDirectory; dir = Path.GetFullPath("../..") + "\\ppt"; string filePath = dir + "\\template.pptx"; Application PPT = new Application();//創建PPT應用 Presentation MyPres = null;//PPT應用的實例 //PPT中的幻燈片 Slide MySlide = null; Slide MySlide2 = null; Slide MySlide3 = null; Slide MySlide4 = null; try { MyPres = PPT.Presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);//打開PPT #region 添加空白頁 MySlide = MyPres.Slides.Add(1, PpSlideLayout.ppLayoutBlank);//第一頁 MySlide2 = MyPres.Slides.Add(2, PpSlideLayout.ppLayoutBlank);//第二頁 MySlide3 = MyPres.Slides.Add(3, PpSlideLayout.ppLayoutBlank);//第三頁 MySlide4 = MyPres.Slides.Add(4, PpSlideLayout.ppLayoutBlank);//第四頁 #endregion #region 添加文本框 TextRange MyTextRng = null; MySlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 21.5F, 365F, 670F, 270F); //請注意此處Shapes的索引,由於文本框是第一個添加的Shapes,所以此處索引是1。 MyTextRng = MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Font.NameFarEast = "微軟雅黑";//文本框中,中文的字體 MyTextRng.Font.NameAscii = "Calibri";//文本框中,英文和數字的字體 MyTextRng.Text = "我是第一頁的文本";//顯示的內容 MyTextRng.Font.Bold = MsoTriState.msoTrue;//是否加粗 MyTextRng.Font.Color.RGB = 123 + 104 * 256 + 238 * 256 * 256;//字體顏色,其中ABC直接用自定義顏色中的數字代替即可。 MyTextRng.Characters(1, 10).Font.Size = 24;//個性化設計。第1個字符開始,長度為10的字符,字體大小是24. MyTextRng.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignLeft;//文本對齊方式(水平方向) MySlide.Shapes[1].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; //文本對齊方式(垂直方向) #endregion #region 添加圖形(矩形) MySlide2.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 8.5F, 6.5F, 705F, 525F); MySlide2.Shapes[1].Line.ForeColor.RGB = 230 + 230 * 256 + 250 * 256 * 256;//改變線條顏色 MySlide2.Shapes[1].Fill.Transparency = 1;//控制填充色為透明 MySlide2.Shapes[1].Line.Style = MsoLineStyle.msoLineSingle;//改變線型里的復合類型 MySlide2.Shapes[1].Line.Weight = 1F;//改變線粗細 MySlide2.Shapes[1].Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制陰影類型 MySlide2.Shapes[1].Shadow.ForeColor.RGB = 0;//控制陰影顏色 MySlide2.Shapes[1].Shadow.Transparency = 0.6F;//控制透明度 MySlide2.Shapes[1].Shadow.Size = 100F;//控制大小 MySlide2.Shapes[1].Shadow.Blur = 4F;//控制虛化 MySlide2.Shapes[1].Shadow.OffsetX = 2.1F;//控制距離; MySlide2.Shapes[1].Shadow.OffsetY = 2.1F;//與offsetX共同決定角度 #endregion #region 添加圖片 string dirImg = AppDomain.CurrentDomain.BaseDirectory; dirImg = Path.GetFullPath("../..") + "\\Image"; string ImageFile = dirImg + "\\草原.jpg"; MySlide3.Shapes.AddPicture(ImageFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, 960F, 540F); #endregion #region 添加表格 Table MyTable = null; MyTable = MySlide4.Shapes.AddTable(19, 5, 0, 0, 960F, 540F).Table;//創建時規定的寬和高,不是表格最終的大小。 MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Font.Size = 10; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Font.Color.RGB = 230 + 230 * 256 + 250 * 256 * 256; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Font.NameAscii = "Arial"; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Font.NameFarEast = "微軟雅黑"; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAlignment.ppAlignCenter; MyTable.Cell(9, 4).Shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; MyTable.Cell(9, 4).Shape.Fill.ForeColor.RGB = 0; MyTable.Cell(9, 4).Shape.TextFrame.TextRange.Text = "C#生成PPT"; #endregion MyPres.SaveAs(dir + "\\test-" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pptx");//保存文件 Console.WriteLine("創建成功!"); Console.ReadKey(); } catch (Exception ex) { throw new Exception(ex.Message); } finally { try { MyPres.Close(); PPT.Quit(); } catch (Exception) { } } } }
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;