2015-06-26 22 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對象被分配到cel2(和cel3)。該對象的相同實例正被分配給cel,cel2cel3,這意味着恰好該實例將與XML樹的不允許的不同部分相關聯。

爲了解決這個問題,您需要爲您創建的每個CellFormat對象分配一個Aligment類的新實例。他們可以分享相同的屬性;他們只需要是不同的實例。

您可以通過顯式創建新實例或使用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 }; 
相關問題