大家好,
前陣子由于作業需要,開始在網上搜集如何利用程式生成PPT。PPT的內容包括文本框,圖形,圖表,表格,圖片等元素。當然了,文本框中內容,圖形的位置,圖表的資料源,表格中的資料都是動態讀入的(這些是后話了)。
本人是個半路出家的碼農。更多的思路主要來源于有經驗的前輩以及網上的資源。在CSDN曾經獲得了很多次的幫助(各位翻看我發的帖子就可以知道)。所以這次也主要是集中在論壇上搜索相關資源,以此來完成任務。
但是,無論是通過專業的論壇,或是直接百度。搜索出來的資源非常少,而且重復性很高。所以最后,我就用了最笨的方法:把 Microsoft.Office.Interop.PowerPoint 類中的相關介面,到微軟官網上去查看檔案,以此來完成添加,設定,修改等操作。
下面就把我的程式貼上來。一來是匯總一下之前所有人的幫助;二來也是想如果還有朋友需要做生成PPT的作業,希望能幫到他們。
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;
上述代碼已成功解決實際問題,所以還沒有時間對其他情況進行研究。
在此,衷心的向那些曾經幫助過我的人表示感謝,同時也感謝CSDN論壇這個平臺,讓我有了學習和交流的地方。
最后,歡迎大家提出批評和意見,共同提高進步。
謝謝大家!
uj5u.com熱心網友回復:
感謝樓主分享
uj5u.com熱心網友回復:
親,怎么沒法使用呢。求指教。uj5u.com熱心網友回復:
@wangcongyi_zz不好意思現在才看到。
能說說您具體的問題嗎?
uj5u.com熱心網友回復:
樓主,請問怎么插入超鏈接呢?uj5u.com熱心網友回復:
怎么對 MyTable.Cell(k, j)的表格邊框進行設定呢?table.Cell(i, j).Shape.Line.Style = MsoLineStyle.msoLineSingle;
這樣設定后提示 "錯誤資訊: 0: 未實作該方法或操作。"
uj5u.com熱心網友回復:
感謝分享,我個人在作業中也用到了相關的技術,參考的是VBA編程的物件、介面、方法,效果也不錯uj5u.com熱心網友回復:
C# 中不能使用VBA吧uj5u.com熱心網友回復:
是不是要手動先創建一個空白ppt ,可不可以通程序式新建uj5u.com熱心網友回復:
MARK一下。感謝樓主分享。uj5u.com熱心網友回復:
好東西,收藏!uj5u.com熱心網友回復:

我碰到這個情況,請問要怎么解決
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/36350.html
標籤:C#
上一篇:新手請教CSharp累計計數問題
