Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Mar 23, 2020 7:20 am

Hi,

I want to create table whose columns will get change based on the data in data table.
how to create Dynamic table in Power point using c#.

AnuH365068
 
Posts: 13
Joined: Wed Mar 04, 2020 10:45 am

Mon Mar 23, 2020 8:37 am

Hello,

Thanks for your inquiry.
Please refer to the below code to create a table dynamically based on the size of the data table. If there is any question, please provide your desired output for our reference.
Code: Select all
        static void Main(string[] args)
        {
            //Create PPT document
            Presentation presentation = new Presentation();

            //Create a datatable
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Rows.Add("Country", "Jun", "Aug");
            dt.Rows.Add("Cuba", "6000", "3200");
            dt.Rows.Add("Mexico", "8000", "2000");
            dt.Rows.Add("France", "9000", "4000");
            dt.Rows.Add("German", "8500", "2300");

            //Create table
            CreateTable(dt, presentation);

            //Save the document
            presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);
        }

        public static void CreateTable(DataTable dt, Presentation presentation)
        {
            //Set table widths and heights
            double[] widths = new double[dt.Columns.Count];
            double[] heights = new double[dt.Rows.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }
            for (int i = 0; i < heights.Length; i++)
            {
                heights[i] = 15;
            }
            //Add new table to PPT
            ITable table = presentation.Slides[0].Shapes.AppendTable(
                presentation.SlideSize.Size.Width / 2 - 275, 80, widths, heights);
            //Set the style of table
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < dt.Rows.Count; i++)
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //Fill the table with data
                    table[j, i].TextFrame.Text = dt.Rows[i][j].ToString();
                    //Set the Font
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Narrow");
                }

            //Set the alignment of the first row to Center
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
            }
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Mar 23, 2020 12:47 pm

Thanks for your help.
suppose i have a data table whose column numbers are not static it will change based on data. for example:some data table may have 10 columns,some may have 12 columns ,in that case how to create table in power point.

Regards,
ANusha

AnuH365068
 
Posts: 13
Joined: Wed Mar 04, 2020 10:45 am

Tue Mar 24, 2020 2:56 am

Hello,

Thanks for your response.
In my code, I just simulated a sample data table with 4 rows and 3 columns, you could replace it with your own data. Please directly invoke the method "CreateTable" and pass your data table as the parameter "dt". Then it will create a table dynamically based on the number of data table's rows and columns.
If there is any question or misunderstanding, please provide your desired output for our reference.

Sincerely,
Rachel
E-iceblue
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Mar 30, 2020 3:11 am

Hello,

Hope you are doing well.
How is your issue now? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Apr 06, 2020 8:51 am

Hi,

Yes the issue is solved.

Thanks.

AnuH365068
 
Posts: 13
Joined: Wed Mar 04, 2020 10:45 am

Mon Apr 06, 2020 9:23 am

Hi,
while i have created dynamic table in power point.now i am facing one more issue.
like if the numbers of rows in tables gets increase, the table is running out of the slide.how to handle it?

For example: table A contains 20 records ,and it is running out of the slide.how to break the table into 2 slides so that table will not run out of the slide.
i want first 10 rows in one slide another 10 in second slide.

AnuH365068
 
Posts: 13
Joined: Wed Mar 04, 2020 10:45 am

Mon Apr 06, 2020 10:30 am

Hi,

Thanks for your further inquiry.
The behavior that the table is running out of the slide when the numbers of rows in tables gets increase is the same as manually operating in MS PowerPoint.
The only solution to remove the rows which are running out of the slide, and then create a new table in the new slide and add the rows into it.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Presentation

cron