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.

Tue Jan 21, 2020 4:11 pm

Greetings folks, I'm in a bit of a spot here hoping you can help. I have data in my SQL Server database that I'm pulling into a data table. What I'm looking to do is apply the values from the data table as the data array for the data string call in code. Your example from https://www.e-iceblue.com/Tutorials/Spi ... Slide.html shows using hard-coded values however I need this to be dynamically populated using code. How can I do that? Many thanks!
Dan Brewerton, MIS
Cybersecurity Specialist
.NET Programmer
User avatar

dbrewerton
 
Posts: 12
Joined: Thu Nov 14, 2019 4:07 pm
Location: Honeoye, NY, USA

Wed Jan 22, 2020 7:55 am

Hello,

Thanks for your post.
The following code demonstrates how 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
    //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");

    //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;
    }
    //Save the document
    presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);
    System.Diagnostics.Process.Start("table.pptx");


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Feb 05, 2020 10:00 am

Hello,

Greetings from E-iceblue!
Did my code help you? 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

Fri Mar 05, 2021 9:01 am

Hi Rachel,

If there are no records then how can we show a message as 'No Records found' in Datatable?

Thanks in advance for your inputs.

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Fri Mar 05, 2021 10:02 am

Hello,

Thanks for your inquiry.
Regarding “no records” you mentioned, do you mean that there are no records in your database when trying to pull the data into the datatable?
If so, please refer to the following code to add a new row with the message “No Records found” to the datatable.

Code: Select all

            DataTable dataTable = new DataTable();
           
           //Fill the results from the database into a DataTable
           //...

            if (dataTable.Rows.Count == 0)
            {
                DataRow row = dataTable.Rows.Add();
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    row[i] = "No Records found";

                }
            }



Or if I misunderstood, please describe your requirement in detail. Thanks in advance.

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Fri Mar 05, 2021 12:10 pm

But, If we have three columns like, Name | Age | Country, then in second row it is binding 'No Record found' in all the three columns.
Rather, it should bind in 2nd row in center 'No Records found'.

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Mon Mar 08, 2021 10:24 am

Hello,

Thanks for your feedback.
Please refer to the following modified code. If this is not what you want, please provide your desired output (the PPTX file or some screenshots) for our better reference. Thanks in advance.
Code: Select all
       static void Main(string[] args)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add();
            dataTable.Columns.Add();
            dataTable.Columns.Add();
            dataTable.Columns.Add();


            if (dataTable.Rows.Count == 0)
            {
                DataRow row = dataTable.Rows.Add();
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    if (dataTable.Columns.Count % 2 == 1)
                    {
                        row[(dataTable.Columns.Count /2)] = 9;
                    }
                    else
                    {
                        row[(dataTable.Columns.Count / 2)-1] = 9;
                    }
                }
            }

            ppt(dataTable);
        }
        private static void ppt(DataTable dt)
        {
            //create PPT document
            Presentation presentation = new 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;
            }
            //Save the document
            presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("table.pptx");
        }



Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.Presentation

cron