create dynamic table in powerpoint using openXML with c# and ASP.net -
i have used these links , got working code can use template report(containing placeolders) , generate new pptx report data database. have 4 more placeholders need populate 4 different data tables. using template create new slide , replacing placeholder text tables couldn't figure out. able generate table using below code not in placeholder's location. table appear in center of screen.
links used: https://blogs.msdn.microsoft.com/brian_jones/2008/11/18/creating-a-presentation-report-based-on-data/
https://code.msdn.microsoft.com/office/how-to-add-a-table-with-03578dde
call button click event:
using (presentationdocument presentationdocument = presentationdocument.open(slidename, true)) { //get first slide presentation slidepart intitalslide = presentationdocument.presentationpart.slideparts.first(); addnewslide(presentationdocument, intitalslide, 1045); }
private void addnewslide(presentationdocument presentationdocument, slidepart _slidetemplate, int projectid) { presentationpart parent = presentationdocument.presentationpart; system.data.datatable dt = getvalueforpptreport(projectid); var newslidepart = parent.addnewpart<slidepart>("newslide1"); //copy contents of template slide new slide , attach appropriate layout newslidepart.feeddata(_slidetemplate.getstream(filemode.open)); newslidepart.addpart(_slidetemplate.slidelayoutpart, _slidetemplate.getidofpart(_slidetemplate.slidelayoutpart)); //alter placeholder text in new slide setplaceholder(newslidepart, "txtprojectidname", dt.rows[0]["projname"].tostring()); setplaceholder(newslidepart, "txtprojtype", dt.rows[0]["proj_type"].tostring()); setplaceholder(newslidepart, "txtprobst", dt.rows[0]["proj_problem_state"].tostring()); setplaceholder(newslidepart, "txtgoal", dt.rows[0]["proj_goal_obj"].tostring()); setplaceholder(newslidepart, "txtinscope", dt.rows[0]["proj_in_scope"].tostring()); setplaceholder(newslidepart, "txtoutofscope", dt.rows[0]["proj_out_scope"].tostring()); setplaceholder(newslidepart, "txtcustomer", dt.rows[0]["proj_who_customer"].tostring()); setplaceholder(newslidepart, "txtctq", dt.rows[0]["proj_critical_to_qlty"].tostring()); setplaceholder(newslidepart, "txtdefect", dt.rows[0]["proj_what_defect"].tostring()); setplaceholder(newslidepart, "txtdate", system.datetime.now.tostring("mm/dd/yyyy hh:mm")); //add tables here //tblxbenefit system.data.datatable dtxb = getvalueforpptreportbenefit(1045); string placeholder = "tblxbenefit"; list<d.text> textlistexif1 = newslidepart.slide.descendants<d.text>().where(t => t.text.equals(placeholder)).tolist(); if (textlistexif1.count == 1) { } list<openxmlelement> elements = new list<openxmlelement>(); elements.add(new p.nonvisualgraphicframeproperties (new p.nonvisualdrawingproperties() { id = 1, name = "xyz" }, new p.nonvisualgraphicframedrawingproperties(), new applicationnonvisualdrawingproperties())); // declare , instantiate graphic frame of new slide p.graphicframe graphicframe = newslidepart.slide.commonslidedata.shapetree.appendchild(new p.graphicframe()); // specify required frame properties of graphicframe applicationnonvisualdrawingpropertiesextension applicationnonvisualdrawingpropertiesextension = new applicationnonvisualdrawingpropertiesextension() { uri = "{d42a27db-bd31-4b8c-83a1-f6eecf244321}" }; p14.modificationid modificationid1 = new p14.modificationid() { val = 3229994563u }; modificationid1.addnamespacedeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main"); applicationnonvisualdrawingpropertiesextension.append(modificationid1); graphicframe.nonvisualgraphicframeproperties = new p.nonvisualgraphicframeproperties (new d.nonvisualdrawingproperties() { id = 5, name = "table 1" }, new d.nonvisualgraphicframedrawingproperties(new d.graphicframelocks() { nogrouping = true }), new applicationnonvisualdrawingproperties(new applicationnonvisualdrawingpropertiesextensionlist(applicationnonvisualdrawingpropertiesextension))); graphicframe.transform = new transform(new d.offset() { x = 1650609l, y = 4343400l }, new d.extents() { cx = 6096000l, cy = 741680l }); // specify griaphic of graphic frame graphicframe.graphic = new d.graphic(new d.graphicdata(generatetable()) { uri = "http://schemas.openxmlformats.org/drawingml/2006/table" }); //save changes slide newslidepart.slide.save(); //need assign id new slide , add slideidlist //first figure out largest existing id documentformat.openxml.presentation.slideidlist slideidlist = parent.presentation.slideidlist; uint maxslideid = 1; foreach (documentformat.openxml.presentation.slideid slideid in slideidlist.childelements) { if (slideid.id > maxslideid) maxslideid = slideid.id; } //assign id , add new slide @ end of list documentformat.openxml.presentation.slideid newslideid = new documentformat.openxml.presentation.slideid { id = ++maxslideid, relationshipid = parent.getidofpart(newslidepart) }; slideidlist.append(newslideid); //delete first template slide slideid tempslideid = slideidlist.childelements[0] slideid; slideidlist.removechild(tempslideid); } private void setplaceholder(slidepart slidepart, string placeholder, string value) { list<d.text> textlistexif1 = slidepart.slide.descendants<d.text>().where(t => t.text.equals(placeholder)).tolist(); foreach (d.text text in textlistexif1) { text.text = value; } } #region tables /// <summary> /// generate table below order: /// a:tbl(table) ->a:tr(tablerow)->a:tc(tablecell) /// can return tablecell object createtextcell method /// , append tablecell object tablerow /// </summary> /// <returns>table object</returns> private static d.table generatetable() { string[,] tablesources = new string[,] { { "name", "age" }, { "tom", "25" } }; // declare , instantiate table d.table table = new d.table(); // specify required table properties table d.tableproperties tableproperties = new d.tableproperties() { firstrow = true, bandrow = true }; d.tablestyleid tablestyleid = new d.tablestyleid(); tablestyleid.text = "{5c22544a-7ee6-4342-b048-85bdc9fd1c3a}"; tableproperties.append(tablestyleid); // declare , instantiate tablegrid , colums d.tablegrid tablegrid1 = new d.tablegrid(); d.gridcolumn gridcolumn1 = new d.gridcolumn() { width = 3048000l }; d.gridcolumn gridcolumn2 = new d.gridcolumn() { width = 3048000l }; tablegrid1.append(gridcolumn1); tablegrid1.append(gridcolumn2); table.append(tableproperties); table.append(tablegrid1); (int row = 0; row < tablesources.getlength(0); row++) { // instantiate table row d.tablerow tablerow = new d.tablerow() { height = 370840l }; (int column = 0; column < tablesources.getlength(1); column++) { tablerow.append(createtextcell(tablesources.getvalue(row, column).tostring())); } table.append(tablerow); } return table; } /// <summary> /// create table cell below order: /// a:tc(tablecell)->a:txbody(textbody)->a:p(paragraph)->a:r(run)->a:t(text) /// </summary> /// <param name="text">inserted text in cell</param> /// <returns>return tablecell object</returns> private static d.tablecell createtextcell(string text) { if (string.isnullorempty(text)) { text = string.empty; } // declare , instantiate table cell // create table cell below order: // a:tc(tablecell)->a:txbody(textbody)->a:p(paragraph)->a:r(run)->a:t(text) d.tablecell tablecell = new d.tablecell(); // declare , instantiate text body d.textbody textbody = new d.textbody(); d.bodyproperties bodyproperties = new d.bodyproperties(); d.liststyle liststyle = new d.liststyle(); d.paragraph paragraph = new d.paragraph(); d.run run = new d.run(); d.runproperties runproperties = new d.runproperties() { language = "en-us", dirty = false }; d.text text2 = new d.text(); text2.text = text; run.append(runproperties); run.append(text2); d.endparagraphrunproperties endparagraphrunproperties = new d.endparagraphrunproperties() { language = "en-us", dirty = false }; paragraph.append(run); paragraph.append(endparagraphrunproperties); textbody.append(bodyproperties); textbody.append(liststyle); textbody.append(paragraph); d.tablecellproperties tablecellproperties = new d.tablecellproperties(); tablecell.append(textbody); tablecell.append(tablecellproperties); return tablecell; } #endregion
okay, can place table @ location want changing values in this.. x , y.. not sure cx , cy values does. works
graphicframe.transform = new transform(new d.offset() { x = 1650609l, y = 4343400l }, new d.extents() { cx = 6096000l, cy = 741680l });
but new issue unable reduce font , table row height. tried changing values nothing works. post here when find out.
d.tablerow tablerow = new d.tablerow() { height = 370840l };
Comments
Post a Comment