npoi
836 строк · 25.3 Кб
1using NPOI.OpenXml4Net.Util;2using NPOI.OpenXmlFormats.Dml.Spreadsheet;3using System;4using System.Collections.Generic;5using System.ComponentModel;6using System.IO;7using System.Text;8using System.Xml;9using System.Xml.Serialization;10
11namespace NPOI.OpenXmlFormats.Dml.Spreadsheet12{
13[Serializable]14[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]15public class CT_Shape // empty interface: EG_ObjectChoices16{17private CT_ShapeNonVisual nvSpPrField;18private CT_ShapeProperties spPrField;19private CT_ShapeStyle styleField;20private CT_TextBody txBodyField;21
22private string macroField;23private string textlinkField;24private bool fLocksTextField;25private bool fPublishedField;26
27public void Set(CT_Shape obj)28{29this.macroField = obj.macro;30this.textlinkField = obj.textlink;31this.fLocksTextField = obj.fLocksText;32this.fPublishedField = obj.fPublished;33
34this.nvSpPrField = obj.nvSpPr;35this.spPrField = obj.spPr;36this.styleField = obj.style;37this.txBodyField = obj.txBody;38}39public static CT_Shape Parse(XmlNode node, XmlNamespaceManager namespaceManager)40{41if (node == null)42return null;43CT_Shape ctObj = new CT_Shape();44ctObj.macro = XmlHelper.ReadString(node.Attributes["macro"]);45ctObj.textlink = XmlHelper.ReadString(node.Attributes["textlink"]);46ctObj.fLocksText = XmlHelper.ReadBool(node.Attributes["fLocksText"]);47ctObj.fPublished = XmlHelper.ReadBool(node.Attributes["fPublished"]);48foreach (XmlNode childNode in node.ChildNodes)49{50if (childNode.LocalName == "nvSpPr")51ctObj.nvSpPr = CT_ShapeNonVisual.Parse(childNode, namespaceManager);52else if (childNode.LocalName == "spPr")53ctObj.spPr = CT_ShapeProperties.Parse(childNode, namespaceManager);54else if (childNode.LocalName == "txBody")55ctObj.txBody = CT_TextBody.Parse(childNode, namespaceManager);56else if (childNode.LocalName == "style")57ctObj.style = CT_ShapeStyle.Parse(childNode, namespaceManager);58}59return ctObj;60}61
62
63
64internal void Write(StreamWriter sw, string nodeName)65{66sw.Write(string.Format("<xdr:{0}", nodeName));67XmlHelper.WriteAttribute(sw, "macro", this.macro, true);68XmlHelper.WriteAttribute(sw, "textlink", this.textlink,true);69XmlHelper.WriteAttribute(sw, "fLocksText", this.fLocksText, false);70XmlHelper.WriteAttribute(sw, "fPublished", this.fPublished, false);71sw.Write(">");72if (this.nvSpPr != null)73this.nvSpPr.Write(sw, "nvSpPr");74if (this.spPr != null)75this.spPr.Write(sw, "spPr");76if (this.style != null)77this.style.Write(sw, "style");78if (this.txBody != null)79this.txBody.Write(sw, "txBody");80sw.Write(string.Format("</xdr:{0}>", nodeName));81}82
83public CT_ShapeNonVisual AddNewNvSpPr()84{85this.nvSpPrField = new CT_ShapeNonVisual();86return this.nvSpPrField;87}88
89public CT_ShapeProperties AddNewSpPr()90{91this.spPrField = new CT_ShapeProperties();92return this.spPrField;93}94public CT_ShapeStyle AddNewStyle()95{96this.styleField = new CT_ShapeStyle();97return this.styleField;98}99public CT_TextBody AddNewTxBody()100{101this.txBodyField = new CT_TextBody();102return this.txBodyField;103}104
105public CT_ShapeNonVisual nvSpPr106{107get108{109return this.nvSpPrField;110}111set112{113this.nvSpPrField = value;114}115}116public CT_ShapeProperties spPr117{118get119{120return this.spPrField;121}122set123{124this.spPrField = value;125}126}127public CT_TextBody txBody128{129get130{131return this.txBodyField;132}133set134{135this.txBodyField = value;136}137}138public CT_ShapeStyle style139{140get141{142return this.styleField;143}144set145{146this.styleField = value;147}148}149
150[XmlAttribute]151public string macro152{153get { return macroField; }154set { macroField = value; }155}156
157[XmlAttribute]158public string textlink159{160get { return textlinkField; }161set { textlinkField = value; }162}163[XmlAttribute]164public bool fLocksText165{166get { return fLocksTextField; }167set { fLocksTextField = value; }168}169
170[XmlAttribute]171public bool fPublished172{173get { return fPublishedField; }174set { fPublishedField = value; }175}176
177}178
179[System.ComponentModel.DesignerCategory("code")]180[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]181public class CT_TextBody182{183
184private CT_TextBodyProperties bodyPrField;185
186private CT_TextListStyle lstStyleField;187
188private List<CT_TextParagraph> pField;189
190public static CT_TextBody Parse(XmlNode node, XmlNamespaceManager namespaceManager)191{192if (node == null)193return null;194CT_TextBody ctObj = new CT_TextBody();195ctObj.p = new List<CT_TextParagraph>();196foreach (XmlNode childNode in node.ChildNodes)197{198if (childNode.LocalName == "bodyPr")199ctObj.bodyPr = CT_TextBodyProperties.Parse(childNode, namespaceManager);200else if (childNode.LocalName == "lstStyle")201ctObj.lstStyle = CT_TextListStyle.Parse(childNode, namespaceManager);202else if (childNode.LocalName == "p")203ctObj.p.Add(CT_TextParagraph.Parse(childNode, namespaceManager));204}205return ctObj;206}207
208
209
210internal void Write(StreamWriter sw, string nodeName)211{212sw.Write(string.Format("<xdr:{0}", nodeName));213sw.Write(">");214if (this.bodyPr != null)215this.bodyPr.Write(sw, "bodyPr");216if (this.lstStyle != null)217this.lstStyle.Write(sw, "lstStyle");218foreach (CT_TextParagraph x in this.p)219{220x.Write(sw, "p");221}222sw.Write(string.Format("</xdr:{0}>", nodeName));223}224
225public void SetPArray(CT_TextParagraph[] array)226{227if (array == null)228pField.Clear();229else230pField = new List<CT_TextParagraph>(array);231}232public CT_TextParagraph AddNewP()233{234if (this.pField == null)235pField = new List<CT_TextParagraph>();236CT_TextParagraph tp = new CT_TextParagraph();237pField.Add(tp);238return tp;239}240public CT_TextBodyProperties AddNewBodyPr()241{242this.bodyPrField = new CT_TextBodyProperties();243return this.bodyPrField;244}245public CT_TextListStyle AddNewLstStyle()246{247this.lstStyleField = new CT_TextListStyle();248return this.lstStyleField;249}250
251public CT_TextBodyProperties bodyPr252{253get254{255return this.bodyPrField;256}257set258{259this.bodyPrField = value;260}261}262
263
264public CT_TextListStyle lstStyle265{266get267{268return this.lstStyleField;269}270set271{272this.lstStyleField = value;273}274}275public override string ToString()276{277if (p == null || p.Count == 0)278return string.Empty;279StringBuilder sb = new StringBuilder();280foreach (CT_TextParagraph tp in p)281{282foreach (CT_RegularTextRun tr in tp.r)283{284sb.Append(tr.t);285}286}287return sb.ToString();288}289
290[XmlElement("p")]291public List<CT_TextParagraph> p292{293get294{295return this.pField;296}297set298{299this.pField = value;300}301}302
303public CT_TextParagraph GetPArray(int pos)304{305if (p == null)306return null;307return p[pos];308}309
310public int SizeOfPArray()311{312return p.Count;313}314}315
316[Serializable]317[System.ComponentModel.DesignerCategory("code")]318[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]319public class CT_ShapeStyle320{321
322private CT_StyleMatrixReference lnRefField;323
324private CT_StyleMatrixReference fillRefField;325
326private CT_StyleMatrixReference effectRefField;327
328private CT_FontReference fontRefField;329public static CT_ShapeStyle Parse(XmlNode node, XmlNamespaceManager namespaceManager)330{331if (node == null)332return null;333CT_ShapeStyle ctObj = new CT_ShapeStyle();334foreach (XmlNode childNode in node.ChildNodes)335{336if (childNode.LocalName == "lnRef")337ctObj.lnRef = CT_StyleMatrixReference.Parse(childNode, namespaceManager);338else if (childNode.LocalName == "fillRef")339ctObj.fillRef = CT_StyleMatrixReference.Parse(childNode, namespaceManager);340else if (childNode.LocalName == "effectRef")341ctObj.effectRef = CT_StyleMatrixReference.Parse(childNode, namespaceManager);342else if (childNode.LocalName == "fontRef")343ctObj.fontRef = CT_FontReference.Parse(childNode, namespaceManager);344}345return ctObj;346}347
348
349
350internal void Write(StreamWriter sw, string nodeName)351{352sw.Write(string.Format("<xdr:{0}", nodeName));353sw.Write(">");354if (this.lnRef != null)355this.lnRef.Write(sw, "lnRef");356if (this.fillRef != null)357this.fillRef.Write(sw, "fillRef");358if (this.effectRef != null)359this.effectRef.Write(sw, "effectRef");360if (this.fontRef != null)361this.fontRef.Write(sw, "fontRef");362sw.Write(string.Format("</xdr:{0}>", nodeName));363}364
365public CT_StyleMatrixReference AddNewFillRef()366{367this.fillRefField = new CT_StyleMatrixReference();368return this.fillRefField;369}370public CT_StyleMatrixReference AddNewLnRef()371{372this.lnRefField = new CT_StyleMatrixReference();373return this.lnRefField;374}375public CT_FontReference AddNewFontRef()376{377this.fontRefField = new CT_FontReference();378return this.fontRefField;379}380public CT_StyleMatrixReference AddNewEffectRef()381{382this.effectRefField = new CT_StyleMatrixReference();383return this.effectRefField;384}385[XmlElement(Order = 0)]386public CT_StyleMatrixReference lnRef387{388get389{390return this.lnRefField;391}392set393{394this.lnRefField = value;395}396}397
398[XmlElement(Order = 1)]399public CT_StyleMatrixReference fillRef400{401get402{403return this.fillRefField;404}405set406{407this.fillRefField = value;408}409}410
411[XmlElement(Order = 2)]412public CT_StyleMatrixReference effectRef413{414get415{416return this.effectRefField;417}418set419{420this.effectRefField = value;421}422}423
424[XmlElement(Order = 3)]425public CT_FontReference fontRef426{427get428{429return this.fontRefField;430}431set432{433this.fontRefField = value;434}435}436}437
438[Serializable]439[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]440public class CT_ShapeNonVisual441{442private CT_NonVisualDrawingProps cNvPrField;443private CT_NonVisualDrawingShapeProps cNvSpPrField;444
445public CT_NonVisualDrawingProps AddNewCNvPr()446{447this.cNvPrField = new CT_NonVisualDrawingProps();448return this.cNvPrField;449}450public CT_NonVisualDrawingShapeProps AddNewCNvSpPr()451{452this.cNvSpPrField = new CT_NonVisualDrawingShapeProps();453return this.cNvSpPrField;454}455public CT_NonVisualDrawingProps cNvPr456{457get458{459return this.cNvPrField;460}461set462{463this.cNvPrField = value;464}465}466
467public CT_NonVisualDrawingShapeProps cNvSpPr468{469get470{471return this.cNvSpPrField;472}473set474{475this.cNvSpPrField = value;476}477}478public static CT_ShapeNonVisual Parse(XmlNode node, XmlNamespaceManager namespaceManager)479{480if (node == null)481return null;482CT_ShapeNonVisual ctObj = new CT_ShapeNonVisual();483foreach (XmlNode childNode in node.ChildNodes)484{485if (childNode.LocalName == "cNvPr")486ctObj.cNvPr = CT_NonVisualDrawingProps.Parse(childNode, namespaceManager);487else if (childNode.LocalName == "cNvSpPr")488ctObj.cNvSpPr = CT_NonVisualDrawingShapeProps.Parse(childNode, namespaceManager);489}490return ctObj;491}492
493
494
495internal void Write(StreamWriter sw, string nodeName)496{497sw.Write(string.Format("<xdr:{0}", nodeName));498sw.Write(">");499if (this.cNvPr != null)500this.cNvPr.Write(sw, "cNvPr");501if (this.cNvSpPr != null)502this.cNvSpPr.Write(sw, "cNvSpPr");503sw.Write(string.Format("</xdr:{0}>", nodeName));504}505
506
507}508[Serializable]509[System.ComponentModel.DesignerCategoryAttribute("code")]510[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/main")]511[XmlRoot(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/main", IsNullable = true)]512public class CT_NonVisualDrawingShapeProps513{514
515private CT_ShapeLocking spLocksField;516
517private CT_OfficeArtExtensionList extLstField;518
519private bool txBoxField;520public static CT_NonVisualDrawingShapeProps Parse(XmlNode node, XmlNamespaceManager namespaceManager)521{522if (node == null)523return null;524CT_NonVisualDrawingShapeProps ctObj = new CT_NonVisualDrawingShapeProps();525ctObj.txBox = XmlHelper.ReadBool(node.Attributes["txBox"]);526foreach (XmlNode childNode in node.ChildNodes)527{528if (childNode.LocalName == "spLocks")529ctObj.spLocks = CT_ShapeLocking.Parse(childNode, namespaceManager);530else if (childNode.LocalName == "extLst")531ctObj.extLst = CT_OfficeArtExtensionList.Parse(childNode, namespaceManager);532}533return ctObj;534}535
536
537
538internal void Write(StreamWriter sw, string nodeName)539{540sw.Write(string.Format("<xdr:{0}", nodeName));541XmlHelper.WriteAttribute(sw, "txBox", this.txBox, false);542sw.Write(">");543if (this.spLocks != null)544this.spLocks.Write(sw, "spLocks");545if (this.extLst != null)546this.extLst.Write(sw, "extLst");547sw.Write(string.Format("</xdr:{0}>", nodeName));548}549
550
551
552[XmlElement(Order = 0)]553public CT_ShapeLocking spLocks554{555get556{557return this.spLocksField;558}559set560{561this.spLocksField = value;562}563}564
565[XmlElement(Order = 1)]566public CT_OfficeArtExtensionList extLst567{568get569{570return this.extLstField;571}572set573{574this.extLstField = value;575}576}577
578[XmlAttribute]579[DefaultValue(false)]580public bool txBox581{582get583{584return this.txBoxField;585}586set587{588this.txBoxField = value;589}590}591}592[Serializable]593[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]594public class CT_GroupShape595{596CT_GroupShapeProperties grpSpPrField;597CT_GroupShapeNonVisual nvGrpSpPrField;598List<CT_Connector> connectors = null;599List<CT_Picture> pictures = null;600List<CT_Shape> shapes = null;601List<CT_GroupShape> groups = null;602
603public CT_GroupShape()604{605this.connectors = new List<CT_Connector>();606this.pictures = new List<CT_Picture>();607this.shapes = new List<CT_Shape>();608this.groups = new List<CT_GroupShape>();609}610
611public void Set(CT_GroupShape groupShape)612{613this.grpSpPrField = groupShape.grpSpPr;614this.nvGrpSpPrField = groupShape.nvGrpSpPr;615}616
617public CT_GroupShapeProperties AddNewGrpSpPr()618{619this.grpSpPrField = new CT_GroupShapeProperties();620return this.grpSpPrField;621}622public CT_GroupShapeNonVisual AddNewNvGrpSpPr()623{624this.nvGrpSpPrField = new CT_GroupShapeNonVisual();625return this.nvGrpSpPrField;626}627public CT_Connector AddNewCxnSp()628{629var connectorField = new CT_Connector();630connectors.Add(connectorField);631return connectorField;632}633public CT_Shape AddNewSp()634{635var shapeField = new CT_Shape();636shapes.Add(shapeField);637return shapeField;638}639public CT_Picture AddNewPic()640{641var pic=new CT_Picture();642pictures.Add(pic);643return pic;644}645public CT_GroupShape AddNewGroup()646{647var group = new CT_GroupShape();648groups.Add(group);649return group;650}651
652public CT_GroupShapeNonVisual nvGrpSpPr653{654get { return nvGrpSpPrField; }655set { nvGrpSpPrField = value; }656}657public CT_GroupShapeProperties grpSpPr658{659get { return grpSpPrField; }660set { grpSpPrField = value; }661
662}663public List<CT_Connector> Connectors { get => connectors; }664public List<CT_Picture> Pictures { get => pictures; }665public List<CT_Shape> Shapes { get => shapes; }666public List<CT_GroupShape> Groups { get => groups; }667
668public void GetShapes(List<object> aShapes) // a:Argument669{670aShapes.Add(this);671if(connectors != null)672{673foreach(var c in connectors)674{675aShapes.Add(c);676}677}678if(pictures != null)679{680foreach(var p in pictures)681{682aShapes.Add(p);683}684}685if(shapes != null)686{687foreach(var s in shapes)688{689aShapes.Add(s);690}691}692if(groups != null)693{694foreach(var shp in groups)695{696shp.GetShapes(aShapes);697}698}699}700
701public static CT_GroupShape Parse(XmlNode node, XmlNamespaceManager namespaceManager)702{703if (node == null)704return null;705CT_GroupShape ctObj = new CT_GroupShape();706foreach (XmlNode childNode in node.ChildNodes)707{708if (childNode.LocalName == "nvGrpSpPr")709ctObj.nvGrpSpPr = CT_GroupShapeNonVisual.Parse(childNode, namespaceManager);710else if (childNode.LocalName == "grpSpPr")711ctObj.grpSpPr = CT_GroupShapeProperties.Parse(childNode, namespaceManager);712else if (childNode.LocalName == "pic")713{714var pic = CT_Picture.Parse(childNode, namespaceManager);715ctObj.pictures.Add(pic);716}717else if (childNode.LocalName == "sp")718{719var shape = CT_Shape.Parse(childNode, namespaceManager);720ctObj.shapes.Add(shape);721}722else if (childNode.LocalName == "cxnSp")723{724var connector = CT_Connector.Parse(childNode, namespaceManager);725ctObj.connectors.Add(connector);726}727else if (childNode.LocalName == "grpSp")728{729var group = CT_GroupShape.Parse(childNode, namespaceManager);730ctObj.groups.Add(group);731}732}733return ctObj;734}735
736
737
738internal void Write(StreamWriter sw, string nodeName)739{740sw.Write(string.Format("<xdr:{0}", nodeName));741sw.Write(">");742if (this.nvGrpSpPr != null)743this.nvGrpSpPr.Write(sw, "xdr:nvGrpSpPr");744if (this.grpSpPr != null)745this.grpSpPr.Write(sw, "xdr:grpSpPr");746if (this.shapes.Count > 0)747{748foreach (var shape in this.shapes)749{750shape.Write(sw, "sp");751}752}753if (this.pictures.Count > 0)754{755foreach (var pic in this.pictures)756{757pic.Write(sw, "pic");758}759}760if (this.connectors.Count > 0)761{762foreach(var con in this.connectors)763{764con.Write(sw, "cxnSp");765}766}767if (this.groups.Count > 0)768{769foreach(var group in this.groups)770{771group.Write(sw, "grpSp");772}773}774sw.Write(string.Format("</xdr:{0}>", nodeName));775}776
777}778
779[Serializable]780[XmlType(Namespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing")]781public class CT_GroupShapeNonVisual782{783CT_NonVisualDrawingProps cNvPrField;784CT_NonVisualGroupDrawingShapeProps cNvGrpSpPrField;785public static CT_GroupShapeNonVisual Parse(XmlNode node, XmlNamespaceManager namespaceManager)786{787if (node == null)788return null;789CT_GroupShapeNonVisual ctObj = new CT_GroupShapeNonVisual();790foreach (XmlNode childNode in node.ChildNodes)791{792if (childNode.LocalName == "cNvPr")793ctObj.cNvPr = CT_NonVisualDrawingProps.Parse(childNode, namespaceManager);794else if (childNode.LocalName == "cNvGrpSpPr")795ctObj.cNvGrpSpPr = CT_NonVisualGroupDrawingShapeProps.Parse(childNode, namespaceManager);796}797return ctObj;798}799
800
801
802internal void Write(StreamWriter sw, string nodeName)803{804sw.Write(string.Format("<{0}", nodeName));805sw.Write(">");806if (this.cNvPr != null)807this.cNvPr.Write(sw, "cNvPr");808if (this.cNvGrpSpPr != null)809this.cNvGrpSpPr.Write(sw, "xdr:cNvGrpSpPr");810sw.Write(string.Format("</{0}>", nodeName));811}812
813public CT_NonVisualGroupDrawingShapeProps AddNewCNvGrpSpPr()814{815this.cNvGrpSpPrField = new CT_NonVisualGroupDrawingShapeProps();816return this.cNvGrpSpPrField;817}818public CT_NonVisualDrawingProps AddNewCNvPr()819{820this.cNvPrField = new CT_NonVisualDrawingProps();821return this.cNvPrField;822}823
824public CT_NonVisualDrawingProps cNvPr825{826get { return cNvPrField; }827set { cNvPrField = value; }828}829public CT_NonVisualGroupDrawingShapeProps cNvGrpSpPr830{831get { return cNvGrpSpPrField; }832set { cNvGrpSpPrField = value; }833}834}835
836}
837