2017-06-14 88 views
1

我想通过OpenXml在PowerPoint中更改表格的顶部边框,但它没有为我工作。单元格当前有一个左,右和底部边框,但是当我尝试复制底部边框并将其添加到顶部边框时,PowerPoint不反映更改。如何在PowerPoint中使用openxml将边框添加到单元格中?

我需要改变什么或者我做错了什么使它工作?

我目前有以下代码复制下边框并将其替换。

BottomBorderLineProperties btp = (BottomBorderLineProperties)celda.TableCellProperties.BottomBorderLineProperties.CloneNode(true); 

    TopBorderLineProperties tbp = new TopBorderLineProperties() 
    { 
     Alignment = btp.Alignment, 
     CapType = btp.CapType, 
     CompoundLineType = btp.CompoundLineType, 
     MCAttributes = btp.MCAttributes, 
     Width = btp.Width 
    }; 

    foreach(OpenXmlElement element in btp.ChildElements) 
    { 
     tbp.Append(element.CloneNode(true)); 
    } 

    celda.TableCellProperties.TopBorderLineProperties = tbp; 

谢谢!

PS:对不起,我的英语

回答

0

为了设置一个单元格的上边框在PowerPoint表的中间,你必须完成两个步骤:

第1步:设置在底边框直接有关的细胞和上述细胞的

第2步:设置在该小区的上边框(你有那部分)

我使用的OpenXML生产力工具来确定这一点。我用一个简单的1幻灯片PowerPoint文件命名为Before.pptx,其中一个表格单元格具有左边框,右边框和右边框。

enter image description here

然后,我添加上边框(使用PowerPoint 2016)和将文件保存为After.pptx。然后,我使用生产力工具来分析这两个文件,并对Before.pptx看起来像After.pptx所需的C#代码进行反向工程。您所需要的重要的代码显示在这里:

 //STEP 1 CODE STARTS HERE 
     A.Table table1=graphicData1.GetFirstChild<A.Table>(); 

     A.TableRow tableRow1=table1.GetFirstChild<A.TableRow>(); 
     A.TableRow tableRow2=table1.Elements<A.TableRow>().ElementAt(1); 

     A.TableCell tableCell1=tableRow1.Elements<A.TableCell>().ElementAt(2); 

     A.TableCellProperties tableCellProperties1=tableCell1.GetFirstChild<A.TableCellProperties>(); 

     A.BottomBorderLineProperties bottomBorderLineProperties1 = new A.BottomBorderLineProperties(){ Width = 12700, CapType = A.LineCapValues.Flat, CompoundLineType = A.CompoundLineValues.Single, Alignment = A.PenAlignmentValues.Center }; 

     A.SolidFill solidFill1 = new A.SolidFill(); 
     A.SchemeColor schemeColor1 = new A.SchemeColor(){ Val = A.SchemeColorValues.Text1 }; 

     solidFill1.Append(schemeColor1); 
     A.PresetDash presetDash1 = new A.PresetDash(){ Val = A.PresetLineDashValues.Solid }; 
     A.Round round1 = new A.Round(); 
     A.HeadEnd headEnd1 = new A.HeadEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium }; 
     A.TailEnd tailEnd1 = new A.TailEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium }; 

     bottomBorderLineProperties1.Append(solidFill1); 
     bottomBorderLineProperties1.Append(presetDash1); 
     bottomBorderLineProperties1.Append(round1); 
     bottomBorderLineProperties1.Append(headEnd1); 
     bottomBorderLineProperties1.Append(tailEnd1); 
     tableCellProperties1.Append(bottomBorderLineProperties1); 
     //STEP 1 CODE ENDS HERE 


     //STEP 2 CODE STARTS HERE 
     A.TableCell tableCell2=tableRow2.Elements<A.TableCell>().ElementAt(2); 

     A.TableCellProperties tableCellProperties2=tableCell2.GetFirstChild<A.TableCellProperties>(); 

     A.BottomBorderLineProperties bottomBorderLineProperties2=tableCellProperties2.GetFirstChild<A.BottomBorderLineProperties>(); 

     A.TopBorderLineProperties topBorderLineProperties1 = new A.TopBorderLineProperties(){ Width = 12700, CapType = A.LineCapValues.Flat, CompoundLineType = A.CompoundLineValues.Single, Alignment = A.PenAlignmentValues.Center }; 

     A.SolidFill solidFill2 = new A.SolidFill(); 
     A.SchemeColor schemeColor2 = new A.SchemeColor(){ Val = A.SchemeColorValues.Text1 }; 

     solidFill2.Append(schemeColor2); 
     A.PresetDash presetDash2 = new A.PresetDash(){ Val = A.PresetLineDashValues.Solid }; 
     A.Round round2 = new A.Round(); 
     A.HeadEnd headEnd2 = new A.HeadEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium }; 
     A.TailEnd tailEnd2 = new A.TailEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium }; 

     topBorderLineProperties1.Append(solidFill2); 
     topBorderLineProperties1.Append(presetDash2); 
     topBorderLineProperties1.Append(round2); 
     topBorderLineProperties1.Append(headEnd2); 
     topBorderLineProperties1.Append(tailEnd2); 
     tableCellProperties2.InsertBefore(topBorderLineProperties1,bottomBorderLineProperties2); 

我跑上面的代码对我的Before.pptx文件和边框完成。

enter image description here

在努力仔细检查,这两个步骤是必须的,我注释掉步骤1的代码并运行它反对Before.pptx文件的最新版本和上边框不见了。这验证了你所看到的问题。因此,绘制一个边界需要两个步骤。

相关问题