2015-06-26 2 views
0

У меня есть следующий код, но я постоянно получаю ошибку «Не удается вставить OpenXmlElement„newChild“, потому что это часть дерева»не удается вставить 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; 

    } 

Есть ли у вас какие-либо идеи ?? Я нашел тему с той же ошибкой, но по какой-то причине она не работает для меня. Любая помощь приветствуется.

+0

сборки в режиме отладки, и дайте нам знать линию, что ошибка в. – jn1kk

ответ

1

Вопрос относится к объекту Alignment, присвоенному cel2cel3). тот же экземпляр этого объекта присваивается cel, cel2 и cel3, что означает, что именно экземпляр будет связан с разными частями дерева 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 }; 
Смежные вопросы