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#.
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;
}
}