2017-08-22 64 views
-1

我有這段代碼。我能夠完美地生成帶有文本數據的pptx報告。本報告中有4個表格,其中包含動態數據。我能夠在PPT中生成一個表格,但無法執行多個表格。無法使用openxml在PPT報告中生成第二個表格

要求:在右邊我有4張桌子。 enter image description here

期望值:我能夠得到第一張表。 enter image description here

在以下代碼我已經在假設將創建表,我可以與PPT數據和地點填充另一幀AddNewSlide方法添加 P.GraphicFrame graphicFrame2。但它不起作用。請幫忙。

using (PresentationDocument presentationDocument = PresentationDocument.Open(slideName, true)) 
      { 
       //Get the first slide from presentation 
       SlidePart intitalSlide = presentationDocument.PresentationPart.SlideParts.First(); 
       AddNewSlide(presentationDocument, intitalSlide, projectID, reportType); 
      } 

#region Powerpoint Reports 


    private System.Data.DataTable GetValueForPPTReport(int projectID) 
    { 
     System.Data.DataTable dt = new System.Data.DataTable(); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "select convert(varchar(10),proj_id) + ' : ' + proj_name as projName,proj_type, proj_problem_state, proj_goal_obj, proj_in_scope, proj_out_scope , proj_who_customer, proj_what_defect,proj_critical_to_qlty from tbl_proj_details where proj_id = @proj_id"; 
     cmd.Parameters.Add("@proj_id", SqlDbType.Int).Value = projectID; 
     dt = (new Bussiness()).Execute(cmd); 
     return dt; 
    } 
    private System.Data.DataTable GetValueForPPTReportBenefit(int projectID) 
    { 
     System.Data.DataTable dt = new System.Data.DataTable(); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "select [proj_ben_type] as [Benefit Type], [proj_ben_year] as Year ,[proj_ben_fcst] as Forecast,[proj_ben_act] as Actual from [tbl_proj_benefits_detail] where proj_id = @proj_id"; 
     cmd.Parameters.Add("@proj_id", SqlDbType.Int).Value = projectID; 
     dt = (new Bussiness()).Execute(cmd); 
     return dt; 
    } 
    private void AddNewSlide(PresentationDocument presentationDocument, SlidePart _slideTemplate, int projectID, string reportType) 
    { 
     PresentationPart parent = presentationDocument.PresentationPart; 
     var newSlidePart = parent.AddNewPart<SlidePart>("newSlide1"); 

     if (reportType == "Charter") 
     { 
      System.Data.DataTable dt = GetValueForPPTReport(projectID); 

      //copy the contents of the template slide to the new slide and attach the appropriate layout 
      newSlidePart.FeedData(_slideTemplate.GetStream(FileMode.Open)); 
      newSlidePart.AddPart(_slideTemplate.SlideLayoutPart, _slideTemplate.GetIdOfPart(_slideTemplate.SlideLayoutPart)); 

      //Alter the 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, "txtDate", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm")); 
     } 


     //Add dynamic Tables here 

     ////List<OpenXmlElement> elements = new List<OpenXmlElement>(); 
     ////elements.Add(new P.NonVisualGraphicFrameProperties 
     //// (new P.NonVisualDrawingProperties() { Id = 1, Name = "xyz" }, new P.NonVisualGraphicFrameDrawingProperties(), new ApplicationNonVisualDrawingProperties())); 

     // Declare and instantiate the graphic Frame of the new slide 
     P.GraphicFrame graphicFrame = newSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame()); 
     P.GraphicFrame graphicFrame2 = newSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame()); 

     // Specify the required Frame properties of the 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); 

     ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension2 = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{D42A27DB-BD31-4B8C-83A1-F6EECF244321}" }; 
     P14.ModificationId modificationId2 = new P14.ModificationId() { Val = 3229994564U }; 
     modificationId2.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main"); 
     applicationNonVisualDrawingPropertiesExtension.Append(modificationId2); 

     graphicFrame.NonVisualGraphicFrameProperties = new P.NonVisualGraphicFrameProperties 
     (new D.NonVisualDrawingProperties() { Id = 1, Name = "table1" }, 
     new D.NonVisualGraphicFrameDrawingProperties(new D.GraphicFrameLocks() { NoGrouping = true }), 
     new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension))); 

     graphicFrame2.NonVisualGraphicFrameProperties = new P.NonVisualGraphicFrameProperties 
     (new D.NonVisualDrawingProperties() { Id = 2, Name = "table2" }, 
     new D.NonVisualGraphicFrameDrawingProperties(new D.GraphicFrameLocks() { NoGrouping = true }), 
     new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension2))); 

     if (true) 
     { 
      graphicFrame.Transform = new Transform(new D.Offset() { X = 5050609L, Y = 883400L }, new D.Extents() { Cx = 0, Cy = 0 }); 
      // Specify the Griaphic of the graphic Frame 
      graphicFrame.Graphic = new D.Graphic(new D.GraphicData(GenerateTable(projectID, reportType)) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" }); 

      graphicFrame2.Transform = new Transform(new D.Offset() { X = 5050609L, Y = 983400L }, new D.Extents() { Cx = 0, Cy = 0 }); 
      // Specify the Griaphic of the graphic Frame 
      graphicFrame2.Graphic = new D.Graphic(new D.GraphicData(GenerateTable(projectID, reportType)) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" }); 
     } 
     //save the changes to the slide 
     newSlidePart.Slide.Save(); 

     //need to assign an id to the new slide and add it to the slideIdList 
     //first figure out the 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 an id and add the new slide at the end of the 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] as 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 as below order: 
    /// a:tbl(Table) ->a:tr(TableRow)->a:tc(TableCell) 
    /// We can return TableCell object with CreateTextCell method 
    /// and Append the TableCell object to TableRow 
    /// </summary> 
    /// <returns>Table Object</returns> 
    private D.Table GenerateTable(int projectID, string reportType) 
    { 
     // Declare and instantiate table 
     D.Table table = new D.Table(); 

     // Specify the required table properties for the table 
     D.TableProperties tableProperties = new D.TableProperties() { FirstRow = true, BandRow = false }; 
     D.TableStyleId tableStyleId = new D.TableStyleId(); 
     tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}"; 

     tableProperties.Append(tableStyleId); 
     D.TableGrid tableGrid1 = new D.TableGrid(); 
     System.Data.DataTable dtData = new System.Data.DataTable(); 
     if (reportType == "Charter") 
     { 
      //tblXBenefit 
      dtData = GetValueForPPTReportBenefit(projectID); 

      // Declare and instantiate tablegrid and colums 
      //D.TableGrid tableGrid1 = new D.TableGrid(); 
      D.GridColumn gridColumn1 = new D.GridColumn() { Width = 1848000L }; 
      D.GridColumn gridColumn2 = new D.GridColumn() { Width = 648000L }; 
      D.GridColumn gridColumn3 = new D.GridColumn() { Width = 648000L }; 
      D.GridColumn gridColumn4 = new D.GridColumn() { Width = 648000L }; 

      tableGrid1.Append(gridColumn1); 
      tableGrid1.Append(gridColumn2); 
      tableGrid1.Append(gridColumn3); 
      tableGrid1.Append(gridColumn4); 
     } 
     table.Append(tableProperties); 
     table.Append(tableGrid1); 

     // Instantiate the table header row 
     D.TableRow tableHRow = new D.TableRow() { Height = 0L }; 
     for (int column = 0; column < dtData.Columns.Count; column++) 
     { 
      tableHRow.Append(CreateTextCell(dtData.Columns[column].ToString())); 
     } 
     table.Append(tableHRow); 

     // Instantiate the table data row 
     for (int row = 0; row < dtData.Rows.Count; row++) 
     { 
      // Instantiate the table row 
      D.TableRow tableRow = new D.TableRow() { Height = 0L }; 
      for (int column = 0; column < dtData.Columns.Count; column++) 
      { 
       tableRow.Append(CreateTextCell(dtData.Rows[row][column].ToString())); 
      } 
      table.Append(tableRow); 
     } 
     return table; 
    } 

    /// <summary> 
    /// Create table cell with the 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 D.TableCell CreateTextCell(string text) 
    { 
     if (string.IsNullOrEmpty(text)) 
     { 
      text = string.Empty; 
     } 

     // Declare and instantiate the table cell 
     // Create table cell with the below order: 
     // a:tc(TableCell)->a:txbody(TextBody)->a:p(Paragraph)->a:r(Run)->a:t(Text) 
     D.TableCell tableCell = new D.TableCell(); 

     // Declare and instantiate the 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, FontSize = 800 }; 
     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, FontSize = 700 }; 

     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 

    #endregion 
+0

與價值佔位符爲什麼downvote,並試圖關閉? – Sak

+0

你說過你有問題,但是你沒有解釋你爲解決問題所做的努力? –

+0

在上面的代碼中,我嘗試在AddNewSlide方法中添加(P.GraphicFrame graphicFrame2,ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension2),假設它會爲表格創建另一個框架,我可以使用這些框架填充數據並放置在PPT上。但它不起作用,這一切我嘗試過,並試圖找到很多關於這些框架類,但沒有找到一個互聯網上的幫助,但我怎麼可以用它來生成多個表。 – Sak

回答

0

好吧,所以我找不到一種方法來生成多個表。我所做的就是發生在tempaltes表中的佔位符替換

SlidePart.Slide.InnerXml