Friday, August 18, 2017 :: Login  

Insert table into Office Powerpoint programmatically in .NET

Export of data from Excel or other sources and present it in a PowerPoint table show up as a requirement in many of my .net projects. A quick and easy solution to create a PowerPoint presentation with for example sales data from their financial applications is simple to accomplish by using Microsoft Office in your .net application.

To use Powerpoint in your .net application you need to ensure you have the proper references. In this case working with PowerPoint you need to add Microsoft Office Object Library and PowerPoint Object Library.

Office 2007
Microsoft Office 12.0 Object Library
Microsoft PowerPoint 12.0 Object Library

Office 2003
Microsoft Office 11.0 Object Library
Microsoft PowerPoint 11.0 Object Library

Office XP/2002
Microsoft Office 10.0 Object Library
Microsoft PowerPoint 10.0 Object Library


Office 2003/2007

VB
Imports ofc = Microsoft.Office.Core
Imports ppt = Microsoft.Office.Interop.PowerPoint
C#
using ofc = Microsoft.Office.Core;
using ppt = Microsoft.Office.Interop.PowerPoint;

Office XP/2002

VB
Imports ofc = Microsoft.Office.Core
Imports ppt = PowerPoint
C#
using ofc = Microsoft.Office.Core;
using ppt = PowerPoint;

After establishing the reference to the office assemblies, you can call the COM object just like any .NET object. In this tutorial of Powerpoint, simply declare a new Application and a Slide object.

VB
Dim pptApplication As New ppt.Application()
Dim pptSlide As ppt.Slide
C#
ppt.Application pptApplication = new ppt.Application();
ppt.Slide pptSlide;

Then you need to create the presentation:

VB
Dim pptPresentation As ppt.Presentation = pptApplication.Presentations.Add(ofc.MsoTriState.msoFalse)
C#
ppt.Presentation pptPresentation = pptApplication.Presentations.Add(ofc.MsoTriState.msoFalse);

Add a Slide to the presentation:

VB
slide = pptPresentation.Slides.Add(1, ppt.PpSlideLayout.ppLayoutTitle)
C#
slide = pptPresentation.Slides.Add(1, ppt.PpSlideLayout.ppLayoutTitle);



[code]
Microsoft.Office.Interop.Powerpoint.Application oPowerpoint;
Microsoft.Office.Interop.Powerpoint._Slide oSlide;
Microsoft.Office.Interop.Powerpoint._Shape oShape;
[/code]


void InsertPPTTable(string[,] tdata, Microsoft.Office.Interop.PowerPoint._Slide oSlide)
        {

            Microsoft.Office.Interop.PowerPoint.Shape oShape;
            int iRow;
            int iColumn;

            int iRows = tdata.GetLength(0);
            int iColumns = tdata.GetLength(1);

            oShape = oSlide.Shapes.AddTable(iRows, iColumns, 500, 110, 160, 120);

            for (iRow = 1; iRow <= oShape.Table.Rows.Count; iRow++)
                {
                    for (iColumn = 1; iColumn <= oShape.Table.Columns.Count; iColumn++)
                        {
                            oShape.Table.Cell(iRow, iColumn).Shape.TextFrame.TextRange.Text = (string)tdata.GetValue(iRow-1, iColumn-1);//Convert.ToString(iRow) + "/" + Convert.ToString(iColumn);
                            oShape.Table.Cell(iRow, iColumn).Shape.TextFrame.TextRange.Font.Name = "Verdana";
                            oShape.Table.Cell(iRow, iColumn).Shape.TextFrame.TextRange.Font.Size = 8;
                        }
       
                }
           

        }

Privacy Policy www.made4dotnet.com 2006

Map IP Address
Powered byIP2Location.com

   Terms Of Use   ::   Privacy Statement   ::   Copyright (c) 2017 www.made4dotnet.com - .NET Development