Hello there.
First of all I must say that I am a newbie when it comes to Open XML
Here is my problem.
I try personalize this example Adding Repeating Data to PowerPoint but without image:
http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx
I don't have error in my code but I have error in the powerpoint output in the last slide: the last slide is empty.
If I comment this part tbl.Append(tr);
I have correct output powerpoint but empty.
Why? Please check my code below.
If you have link for similar task, please give it me.
Can you explain any one or any sample code related this.Your help would be very appreciated.
Thanks in advance for your time and hints.Cheers.
using System; using System.Globalization; using System.Data; using System.Data.Odbc; using System.Drawing; using System.IO; using System.Collections.Generic; using System.Linq; using System.Configuration; using System.Security.Principal; using System.Text; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Presentation; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Drawing; public partial class _Default2 : System.Web.UI.Page { static int index = 1; string section = ""; string imageRel = "imageRelId"; string ZoneFlight; int imageRelId = 1; int totalHeight = 0; SlidePart current = null; int heightInEmu; int widthInEmu; bool overflow = false; OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { File.Copy(Server.MapPath("pptx/template.pptx"), Server.MapPath("pptx/copy_template.pptx"), true); using (PresentationDocument myPres = PresentationDocument.Open(Server.MapPath("pptx/copy_template.pptx"), true)) { PresentationPart presPart = myPres.PresentationPart; SlidePart sectionSlidePart = (SlidePart)presPart.GetPartById("rId3"); SlidePart tableSlidePart = (SlidePart)presPart.GetPartById("rId4"); myConnectionString.Open(); string cmdSelect = " Select DateFlight " + " ,DtrFlight " + " ,CodeFlight " + " ,CODE_UOFlight " + " ,UOFlight " + " ,CONCATFlight " + " ,DescrFlight " + " ,InstFlight " + " ,ArrFlight " + " ,GAPFlight " + " ,GAPFlight_perc " + " from tbl_Flight group by CODE_UOFlight;"; OdbcCommand objCmd = new OdbcCommand(cmdSelect, myConnectionString); objCmd.Prepare(); objCmd.CommandType = CommandType.Text; objCmd.CommandText = cmdSelect; OdbcDataReader reader = objCmd.ExecuteReader(); while (reader.Read()) { CODE_UOFlight = (reader[3]).ToString(); Response.Write(CODE_UOFlight + "<br />"); } if (section != CODE_UOFlight) { SlidePart newSectionPart = CloneSlidePart(presPart, sectionSlidePart); SwapPlaceholderText(newSectionPart, "Section Title", "myTitle"); section = CODE_UOFlight; overflow = true; } if (overflow) { SlidePart newTablePart = CloneSlidePart(presPart, tableSlidePart); SwapPlaceholderText(newTablePart, "Section", "mySection"); current = newTablePart; overflow = false; totalHeight = 0; } overflow = true; DocumentFormat.OpenXml.Drawing.Table tbl = current.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Table>().First(); DocumentFormat.OpenXml.Drawing.TableRow tr = new DocumentFormat.OpenXml.Drawing.TableRow(); tr.Append(CreateTextCell(CODE_UOFlight)); tbl.Append(tr); DeleteTemplateSlide(presPart, sectionSlidePart, "rId3"); DeleteTemplateSlide(presPart, tableSlidePart, "rId4"); reader.Close(); reader.Dispose(); myConnectionString.Close(); myConnectionString.Dispose(); } } static void CalculateImageEmus(Bitmap bitmap, out int widthInEmu, out int heightInEmu) { float verticalResolution = bitmap.VerticalResolution; float horizontalResolution = bitmap.HorizontalResolution; int width = bitmap.Size.Width; int height = bitmap.Size.Height; float widthInInches = (float)width / horizontalResolution; float heightInInches = (float)height / verticalResolution; widthInEmu = (int)(widthInInches * 914400); heightInEmu = (int)(heightInInches * 914400); } static SlidePart CloneSlidePart(PresentationPart presentationPart, SlidePart slideTemplate) { SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + index); index++; newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open)); newSlidePart.AddPart(slideTemplate.SlideLayoutPart); SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; prevSlideId = slideId; } } maxSlideId++; SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); return newSlidePart; } static void SwapPlaceholderText(SlidePart slidePart, string placeholder, string value) { List<DocumentFormat.OpenXml.Drawing.Text> textList = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Text>() .Where(t => t.Text.Equals(placeholder)).ToList(); foreach (DocumentFormat.OpenXml.Drawing.Text text in textList) text.Text = value; } static void DeleteTemplateSlide(PresentationPart presentationPart, SlidePart slideTemplate, string relId) { SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.RelationshipId.Value.Equals(relId)) slideIdList.RemoveChild(slideId); } presentationPart.DeletePart(slideTemplate); } static DocumentFormat.OpenXml.Drawing.TableCell CreateTextCell(string text) { DocumentFormat.OpenXml.Drawing.TableCell tc = new DocumentFormat.OpenXml.Drawing.TableCell( new DocumentFormat.OpenXml.Drawing.TextBody( new DocumentFormat.OpenXml.Drawing.BodyProperties(), new DocumentFormat.OpenXml.Drawing.Paragraph( new DocumentFormat.OpenXml.Drawing.Run( new DocumentFormat.OpenXml.Drawing.Text(text)))), new DocumentFormat.OpenXml.Drawing.TableCellProperties()); return tc; } static DocumentFormat.OpenXml.Drawing.TableCell CreateDrawingCell(string relId) { DocumentFormat.OpenXml.Drawing.TableCell tc = new DocumentFormat.OpenXml.Drawing.TableCell( new DocumentFormat.OpenXml.Drawing.TextBody( new DocumentFormat.OpenXml.Drawing.BodyProperties(), new DocumentFormat.OpenXml.Drawing.Paragraph()), new DocumentFormat.OpenXml.Drawing.TableCellProperties( new DocumentFormat.OpenXml.Drawing.BlipFill( new DocumentFormat.OpenXml.Drawing.Blip() { Embed = relId }, new DocumentFormat.OpenXml.Drawing.Stretch( new DocumentFormat.OpenXml.Drawing.FillRectangle())))); return tc; } }
Can you help me?