无法插入OpenXmlElement" newChild"

时间:2015-06-26 23:12:12

标签: openxml

我有以下代码,但我一直收到错误 "无法插入OpenXmlElement" newChild"因为它是树的一部分"

private static Stylesheet CreateStylesheet()
    {
        Stylesheet ss = new Stylesheet();
        //ESTILO PARA CELDA F6


        Fills fill1 = new Fills();

        //INDEX 0
        PatternFill fill = new PatternFill() { PatternType = PatternValues.Gray125, BackgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "#92d050" } }, ForegroundColor = new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "#000000" } } };
        GradientFill gfill = new GradientFill() { Type = GradientValues.Path, Degree = 45, Left=0.2, Right= 0.8, Top=0.2, Bottom = 0.8 };
        GradientStop sfill1 = new GradientStop() { Position = 0, Color = new Color() { Theme = 0 }  };
        fill1.Append(fill);
        fill1.Append(gfill);
        fill1.Append(sfill1);

        //ESTILO PARA CELDA F7

        //INDEX 1
        PatternFill fill2 = new PatternFill() { PatternType = PatternValues.Gray125, BackgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "#0a2060" } }, ForegroundColor = new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "#000000" } } };
        GradientFill gfil2 = new GradientFill()  { Type = GradientValues.Path, Degree = 45, Left=0.2, Right= 0.8, Top=0.2, Bottom = 0.8 };
        GradientStop sfill2 = new GradientStop() { Position = 0, Color = new Color() { Theme = 0 } };
        fill1.Append(fill2);
        fill1.Append(gfil2);
        fill1.Append(sfill2);


        //ESTILO GENERAL DE ALTAS

        //INDEX 2
        PatternFill fill3 = new PatternFill() { PatternType = PatternValues.Gray125, BackgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "#92d050" } }, ForegroundColor = new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "#000000" } } };
        GradientFill gfil3 = new GradientFill() { Type = GradientValues.Path, Degree = 45, Left = 0.2, Right = 0.8, Top = 0.2, Bottom = 0.8 };
        GradientStop sfill3 = new GradientStop() { Position = 0, Color = new Color() { Theme = 0 } };
        fill1.Append(fill3);
        fill1.Append(gfil3);
        fill1.Append(sfill3);

        //ESTILO GENERAL DE ANUAL

        //INDEX 3
        PatternFill fill4 = new PatternFill() { PatternType = PatternValues.Gray125, BackgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "#002060" } }, ForegroundColor = new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "#000000" } } };
        GradientFill gfil4 = new GradientFill() { Type = GradientValues.Path, Degree = 45, Left = 0.2, Right = 0.8, Top = 0.2, Bottom = 0.8};
        GradientStop sfill4 = new GradientStop() { Position = 0, Color = new Color() { Theme = 0 } };
        fill1.Append(fill4);
        fill1.Append(gfil4);
        fill1.Append(sfill4);



        //ESTILO DE BORDE PARA INGRESAR UNA CELDA EN EL METODO ASIGNACELDA

        //INDEX 0
        Borders brs = new Borders();
        Border br = new Border() { BottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }, TopBorder = new TopBorder() { Style = BorderStyleValues.Thin }, LeftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }, RightBorder = new RightBorder() { Style = BorderStyleValues.Thin } };
        brs.Append(br);



        //ALINEACIONES


        //ALINEACIÓN PARA EL CELL FORMAT DE CELDA GENERAL EN ASIGNACELDA

        Alignment al = new Alignment() { WrapText = true , Vertical = VerticalAlignmentValues.Center};

       //ALIENACIÓN PARA VALIDACION DE LFA Y LIA
        Alignment al2 = new Alignment() { WrapText = true , ShrinkToFit = true};

        //NUMERACIÓN


        //FORMATO DE NUMEROS NEGATIVOS / POSITIVOS
        NumberingFormats nformats = new NumberingFormats();

        //INDEX 0
        NumberingFormat nfor = new NumberingFormat() { FormatCode = "#,##0.00;[RED]-#,##0.00" };
        nformats.Append(nfor);


        //CELL FORMATS


        CellFormats cells = new CellFormats();

        //CELLFORMAT DE  ASIGNA CELDA GENERAL DE ASIGNACELDA

        //INDEX 0 ESTILO GENERALA DE ALTAS
        CellFormat cel = new CellFormat() {  Alignment = al , BorderId = 0  };



//Here's where the error is generated
-->        CellFormat cel2 = new CellFormat() { Alignment = al , FillId =2 , BorderId = 0 };

"           --- en este segmento es donde manda la expection "

        //INDEX 2 ESTILO  DE ANUAL
        CellFormat cel3 = new CellFormat() { Alignment = al, FillId = 3, BorderId = 0 };


        //INDEX 3 ESTILO PARA INSERTCELLSPECIAL
        CellFormat cel4 = new CellFormat() { NumberFormatId = 0 };

        //INDEX 4 ESTILO PARA VALIDACION LFA Y LIA
        CellFormat cel5 = new CellFormat() { Alignment = al2 };

        cells.Append(cel);
        cells.Append(cel2);
        cells.Append(cel3);
        cells.Append(cel4);
        cells.Append(cel5);



        return ss;

    }

你有什么想法吗?我发现了一个具有相同错误的主题但由于某种原因它对我不起作用。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:1)

问题在于Alignment对象被分配到cel2(和cel3)。该对象的相同实例被分配给celcel2cel3,这意味着该实例将与XML树的不同部分相关联。是不允许的。

要解决此问题,您需要为您创建的每个Aligment对象分配CellFormat类的新实例。他们可以共享相同的属性;他们只需要成为不同的实例。

您可以通过显式创建新实例或使用Clone方法创建Alignment对象的副本来执行此操作。

例如:

Alignment al = new Alignment() { WrapText = true, Vertical = VerticalAlignmentValues.Center };

//safe to use al
CellFormat cel = new CellFormat() { Alignment = al, BorderId = 0 };

//can't use al but we can use a clone of it
CellFormat cel2 = new CellFormat() { Alignment = (Alignment)al.Clone(), FillId = 2, BorderId = 0 };

//this time we're explicitly creating a new Alignment but we could have used clone again
CellFormat cel3 = new CellFormat() { Alignment = new Alignment() { WrapText = true, ShrinkToFit = true }, FillId = 3, BorderId = 0 };