Reputation: 36
I have the following code but i keep getting the error "Cannot insert the OpenXmlElement "newChild" because it is part of a tree"
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;
}
Do you have any ideas?? I found a topic with the same error but for some reason it does not work for me. Any help is greatly appreciated.
Upvotes: 1
Views: 3368
Reputation: 12815
The issue is with the Alignment
object being assigned to cel2
(and cel3
). The same instance of that object is being assigned to cel
, cel2
and cel3
meaning that exactly the instance would be associated with different parts of the XML tree which is not allowed.
To get round the problem you need to assign a new instance of the Aligment
class to each CellFormat
object you create. They can share the same properties; they just need to be different instances.
You can do this by either creating new instances explicitly or by using the Clone
method to create a copy of the Alignment
object.
For example:
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 };
Upvotes: 1