Example #1
0
 /// <summary>
 /// Disposes the object
 /// </summary>
 public void Dispose()
 {
     _drawingsXml = null;
     _hashes.Clear();
     _hashes = null;
     _part   = null;
     _drawingNames.Clear();
     _drawingNames    = null;
     _drawingRelation = null;
     foreach (var d in _drawings)
     {
         d.Dispose();
     }
     _drawings.Clear();
     _drawings = null;
 }
Example #2
0
        internal ExcelPicture(ExcelDrawings drawings, XmlNode node) :
            base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name")
        {
            XmlNode picNode = node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip", drawings.NameSpaceManager);

            if (picNode != null)
            {
                var embed = picNode.Attributes["r:embed"]?.Value;
                if (embed != null)
                {
                    RelPic = drawings.Part.GetRelationship(embed);
                    UriPic = UriHelper.ResolvePartUri(drawings.UriDrawing, RelPic.TargetUri);

                    Part = drawings.Part.Package.GetPart(UriPic);
                    FileInfo f = new FileInfo(UriPic.OriginalString);
                    ContentType = GetContentType(f.Extension);
                    _image      = Image.FromStream(Part.GetStream());
                    ImageConverter ic  = new ImageConverter();
                    var            iby = (byte[])ic.ConvertTo(_image, typeof(byte[]));
                    var            ii  = _drawings.Package.LoadImage(iby, UriPic, Part);
                    ImageHash = ii.Hash;
                }

                var link = picNode.Attributes["r:link"]?.Value;
                if (link != null)
                {
                    RelPic = drawings.Part.GetRelationship(link);
                    // This relationship defines a path to an image file in the file system, and is currently unsupported.
                }
                string relID = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@r:id");
                if (!string.IsNullOrEmpty(relID))
                {
                    HypRel = drawings.Part.GetRelationship(relID);
                    if (HypRel.TargetUri.IsAbsoluteUri)
                    {
                        _hyperlink = new ExcelHyperLink(HypRel.TargetUri.AbsoluteUri);
                    }
                    else
                    {
                        _hyperlink = new ExcelHyperLink(HypRel.TargetUri.OriginalString, UriKind.Relative);
                    }
                    ((ExcelHyperLink)_hyperlink).ToolTip = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@tooltip");
                }
            }
        }
Example #3
0
        internal ExcelDrawings(ExcelPackage xlPackage, ExcelWorksheet sheet)
        {
            _drawingsXml = new XmlDocument();
            _drawingsXml.PreserveWhitespace = false;
            _drawings     = new List <ExcelDrawing>();
            _drawingNames = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);
            _package      = xlPackage;
            Worksheet     = sheet;
            XmlNode node = sheet.WorksheetXml.SelectSingleNode("//d:drawing", sheet.NameSpaceManager);

            CreateNSM();
            if (node != null)
            {
                _drawingRelation = sheet.Part.GetRelationship(node.Attributes["r:id"].Value);
                _uriDrawing      = UriHelper.ResolvePartUri(sheet.WorksheetUri, _drawingRelation.TargetUri);

                _part = xlPackage.Package.GetPart(_uriDrawing);
                XmlHelper.LoadXmlSafe(_drawingsXml, _part.GetStream());

                AddDrawings();
            }
        }
Example #4
0
        internal ExcelPivotTable(Packaging.ZipPackageRelationship rel, ExcelWorksheet sheet) :
            base(sheet.NameSpaceManager)
        {
            WorkSheet     = sheet;
            PivotTableUri = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
            Relationship  = rel;
            var pck = sheet._package.Package;

            Part = pck.GetPart(PivotTableUri);

            PivotTableXml = new XmlDocument();
            LoadXmlSafe(PivotTableXml, Part.GetStream());
            init();
            TopNode = PivotTableXml.DocumentElement;
            Address = new ExcelAddressBase(GetXmlNodeString("d:location/@ref"));

            _cacheDefinition = new ExcelPivotCacheDefinition(sheet.NameSpaceManager, this);
            LoadFields();

            //Add row fields.
            foreach (XmlElement rowElem in TopNode.SelectNodes("d:rowFields/d:field", NameSpaceManager))
            {
                int x;
                if (int.TryParse(rowElem.GetAttribute("x"), out x) && x >= 0)
                {
                    RowFields.AddInternal(Fields[x]);
                }
                else
                {
                    rowElem.ParentNode.RemoveChild(rowElem);
                }
            }

            ////Add column fields.
            foreach (XmlElement colElem in TopNode.SelectNodes("d:colFields/d:field", NameSpaceManager))
            {
                int x;
                if (int.TryParse(colElem.GetAttribute("x"), out x) && x >= 0)
                {
                    ColumnFields.AddInternal(Fields[x]);
                }
                else
                {
                    colElem.ParentNode.RemoveChild(colElem);
                }
            }

            //Add Page elements
            //int index = 0;
            foreach (XmlElement pageElem in TopNode.SelectNodes("d:pageFields/d:pageField", NameSpaceManager))
            {
                int fld;
                if (int.TryParse(pageElem.GetAttribute("fld"), out fld) && fld >= 0)
                {
                    var field = Fields[fld];
                    field._pageFieldSettings = new ExcelPivotTablePageFieldSettings(NameSpaceManager, pageElem, field, fld);
                    PageFields.AddInternal(field);
                }
            }

            //Add data elements
            //index = 0;
            foreach (XmlElement dataElem in TopNode.SelectNodes("d:dataFields/d:dataField", NameSpaceManager))
            {
                int fld;
                if (int.TryParse(dataElem.GetAttribute("fld"), out fld) && fld >= 0)
                {
                    var field     = Fields[fld];
                    var dataField = new ExcelPivotTableDataField(NameSpaceManager, dataElem, field);
                    DataFields.AddInternal(dataField);
                }
            }
        }
Example #5
0
        private XmlElement CreateDrawingXml()
        {
            if (DrawingXml.DocumentElement == null)
            {
                DrawingXml.LoadXml(string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><xdr:wsDr xmlns:xdr=\"{0}\" xmlns:a=\"{1}\" />", ExcelPackage.schemaSheetDrawings, ExcelPackage.schemaDrawings));
                Packaging.ZipPackage package = Worksheet._package.Package;

                //Check for existing part, issue #100
                var id = Worksheet.SheetID;
                do
                {
                    _uriDrawing = new Uri(string.Format("/xl/drawings/drawing{0}.xml", id++), UriKind.Relative);
                }while (package.PartExists(_uriDrawing));

                _part = package.CreatePart(_uriDrawing, "application/vnd.openxmlformats-officedocument.drawing+xml", _package.Compression);

                StreamWriter streamChart = new StreamWriter(_part.GetStream(FileMode.Create, FileAccess.Write));
                DrawingXml.Save(streamChart);
                streamChart.Close();
                package.Flush();

                _drawingRelation = Worksheet.Part.CreateRelationship(UriHelper.GetRelativeUri(Worksheet.WorksheetUri, _uriDrawing), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing");
                XmlElement e = (XmlElement)Worksheet.CreateNode("d:drawing");
                //XmlElement e = Worksheet.WorksheetXml.SelectSingleNode("d:drawing", );
                e.SetAttribute("id", ExcelPackage.schemaRelationships, _drawingRelation.Id);

                //Worksheet.WorksheetXml.DocumentElement.AppendChild(e);
                package.Flush();
            }
            XmlNode    colNode = _drawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager);
            XmlElement drawNode;

            if (this.Worksheet is ExcelChartsheet)
            {
                drawNode = _drawingsXml.CreateElement("xdr", "absoluteAnchor", ExcelPackage.schemaSheetDrawings);
                XmlElement posNode = _drawingsXml.CreateElement("xdr", "pos", ExcelPackage.schemaSheetDrawings);
                posNode.SetAttribute("y", "0");
                posNode.SetAttribute("x", "0");
                drawNode.AppendChild(posNode);
                XmlElement extNode = _drawingsXml.CreateElement("xdr", "ext", ExcelPackage.schemaSheetDrawings);
                extNode.SetAttribute("cy", "6072876");
                extNode.SetAttribute("cx", "9299263");
                drawNode.AppendChild(extNode);
                colNode.AppendChild(drawNode);
            }
            else
            {
                drawNode = _drawingsXml.CreateElement("xdr", "twoCellAnchor", ExcelPackage.schemaSheetDrawings);
                colNode.AppendChild(drawNode);
                //Add from position Element;
                XmlElement fromNode = _drawingsXml.CreateElement("xdr", "from", ExcelPackage.schemaSheetDrawings);
                drawNode.AppendChild(fromNode);
                fromNode.InnerXml = "<xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff>";

                //Add to position Element;
                XmlElement toNode = _drawingsXml.CreateElement("xdr", "to", ExcelPackage.schemaSheetDrawings);
                drawNode.AppendChild(toNode);
                toNode.InnerXml = "<xdr:col>10</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>10</xdr:row><xdr:rowOff>0</xdr:rowOff>";
            }

            return(drawNode);
        }