/// <summary>
        /// Creates an instance of a <see cref="ExcelPivotCacheRecords"/>.
        /// </summary>
        /// <param name="ns">The namespace of the worksheet.</param>
        /// <param name="package">The <see cref="Packaging.ZipPackage"/> of the Excel package.</param>
        /// <param name="tableId">The <see cref="ExcelPivotTable"/>'s ID.</param>
        /// <param name="cacheDefinition">The cache definition of the pivot table.</param>
        public ExcelPivotCacheRecords(XmlNamespaceManager ns, Packaging.ZipPackage package, ref int tableId, ExcelPivotCacheDefinition cacheDefinition) : base(ns, null)
        {
            if (ns == null)
            {
                throw new ArgumentNullException(nameof(ns));
            }
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (cacheDefinition == null)
            {
                throw new ArgumentNullException(nameof(cacheDefinition));
            }
            if (tableId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(tableId));
            }
            // CacheRecord. Create an empty one.
            this.Uri = XmlHelper.GetNewUri(package, $"/xl/pivotCache/{ExcelPivotCacheRecords.Name}{{0}}.xml", ref tableId);
            var cacheRecord = new XmlDocument();

            cacheRecord.LoadXml($"<{ExcelPivotCacheRecords.Name} xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" count=\"0\" />");
            this.Part            = package.CreatePart(this.Uri, ExcelPackage.schemaPivotCacheRecords);
            this.CacheRecordsXml = cacheRecord;
            cacheRecord.Save(this.Part.GetStream());

            base.TopNode         = cacheRecord.FirstChild;
            this.CacheDefinition = cacheDefinition;
        }
Exemple #2
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);
                _part.SaveXml(DrawingXml);
                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);
        }
Exemple #3
0
        internal void Save()
        {
            if (Validate())
            {
                CompoundDocument doc = new CompoundDocument();
                doc.Storage = new CompoundDocument.StoragePart();
                var store = new CompoundDocument.StoragePart();
                doc.Storage.SubStorage.Add("VBA", store);

                store.DataStreams.Add("_VBA_PROJECT", CreateVBAProjectStream());
                store.DataStreams.Add("dir", CreateDirStream());
                foreach (var module in Modules)
                {
                    store.DataStreams.Add(module.Name, CompoundDocument.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code)));
                }

                //Copy streams from the template, if used.
                if (Document != null)
                {
                    foreach (var ss in Document.Storage.SubStorage)
                    {
                        if (ss.Key != "VBA")
                        {
                            doc.Storage.SubStorage.Add(ss.Key, ss.Value);
                        }
                    }
                    foreach (var s in Document.Storage.DataStreams)
                    {
                        if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm")
                        {
                            doc.Storage.DataStreams.Add(s.Key, s.Value);
                        }
                    }
                }

                doc.Storage.DataStreams.Add("PROJECT", CreateProjectStream());
                doc.Storage.DataStreams.Add("PROJECTwm", CreateProjectwmStream());

                if (Part == null)
                {
                    Uri  = new Uri(PartUri, UriKind.Relative);
                    Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA);
                    var rel = _wb.Part.CreateRelationship(Uri, Packaging.TargetMode.Internal, schemaRelVba);
                }
                var vbaBuffer = doc.Save();
                var st        = Part.GetStream(FileMode.Create);
                st.Write(vbaBuffer, 0, vbaBuffer.Length);
                st.Flush();
                //Save the digital signture
                Signature.Save(this);
            }
        }
Exemple #4
0
        private XmlElement CreateDrawingXml()
        {
            if (this.DrawingXml.OuterXml == "")
            {
                this.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));
                this._uriDrawing = XmlHelper.GetNewUri(this.Package.Package, "/xl/drawings/drawing{0}.xml");
                Packaging.ZipPackage package = this.Worksheet.Package.Package;
                this._part = package.CreatePart(this._uriDrawing, "application/vnd.openxmlformats-officedocument.drawing+xml", this.Package.Compression);

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

                this.DrawingRelationship = this.Worksheet.Part.CreateRelationship(UriHelper.GetRelativeUri(this.Worksheet.WorksheetUri, this._uriDrawing), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing");
                XmlElement drawingElement = this.Worksheet.WorksheetXml.CreateElement("drawing", ExcelPackage.schemaMain);
                drawingElement.SetAttribute("id", ExcelPackage.schemaRelationships, this.DrawingRelationship.Id);

                this.Worksheet.WorksheetXml.DocumentElement.AppendChild(drawingElement);
                package.Flush();
            }
            XmlNode    columnNode = this._drawingsXml.SelectSingleNode("//xdr:wsDr", this.NameSpaceManager);
            XmlElement drawingNode;

            if (this.Worksheet is ExcelChartsheet)
            {
                drawingNode = this._drawingsXml.CreateElement("xdr", "absoluteAnchor", ExcelPackage.schemaSheetDrawings);
                XmlElement posNode = this._drawingsXml.CreateElement("xdr", "pos", ExcelPackage.schemaSheetDrawings);
                posNode.SetAttribute("y", "0");
                posNode.SetAttribute("x", "0");
                drawingNode.AppendChild(posNode);
                XmlElement extNode = this._drawingsXml.CreateElement("xdr", "ext", ExcelPackage.schemaSheetDrawings);
                extNode.SetAttribute("cy", "6072876");
                extNode.SetAttribute("cx", "9299263");
                drawingNode.AppendChild(extNode);
                columnNode.AppendChild(drawingNode);
            }
            else
            {
                drawingNode = this._drawingsXml.CreateElement("xdr", "twoCellAnchor", ExcelPackage.schemaSheetDrawings);
                columnNode.AppendChild(drawingNode);
                XmlElement fromNode = this._drawingsXml.CreateElement("xdr", "from", ExcelPackage.schemaSheetDrawings);
                drawingNode.AppendChild(fromNode);
                fromNode.InnerXml = "<xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff>";
                XmlElement toNode = this._drawingsXml.CreateElement("xdr", "to", ExcelPackage.schemaSheetDrawings);
                drawingNode.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(drawingNode);
        }
        internal void Save()
        {
            if (Validate())
            {
                CompoundDocument doc = new CompoundDocument(this._wb._package.tempFolder);
                doc.Storage = new CompoundDocument.StoragePart();
                var store = new CompoundDocument.StoragePart();
                doc.Storage.SubStorage.Add("VBA", store);

                store.DataStreams.Add("_VBA_PROJECT", new MemoryStream(CreateVBAProjectStream()));
                store.DataStreams.Add("dir", new MemoryStream(CreateDirStream()));
                foreach (var module in Modules)
                {
                    store.DataStreams.Add(module.Name, new MemoryStream(VBACompression.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code))));
                }

                //Copy streams from the template, if used.
                if (Document != null)
                {
                    foreach (var ss in Document.Storage.SubStorage)
                    {
                        if (ss.Key != "VBA")
                        {
                            doc.Storage.SubStorage.Add(ss.Key, ss.Value);
                        }
                    }
                    foreach (var s in Document.Storage.DataStreams)
                    {
                        if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm")
                        {
                            doc.Storage.DataStreams.Add(s.Key, s.Value);
                        }
                    }
                }

                doc.Storage.DataStreams.Add("PROJECT", new MemoryStream(CreateProjectStream()));
                doc.Storage.DataStreams.Add("PROJECTwm", new MemoryStream(CreateProjectwmStream()));

                if (Part == null)
                {
                    Uri  = new Uri(PartUri, UriKind.Relative);
                    Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA);
                    var rel = _wb.Part.CreateRelationship(Uri, Packaging.TargetMode.Internal, schemaRelVba);
                }
                var st = Part.GetStream(FileMode.Create);
                doc.Save(st);
                st.Flush();

                /*TODO Stream fs = null;
                 * try
                 * {
                 *  fs = new FileStream(_pck.GetTempFile(), FileMode.Create);
                 *  doc.Save(fs);
                 * }
                 * finally
                 * {
                 *  if (fs != null)
                 *      fs.Dispose();
                 * }
                 * Stream st = Part.GetStream(FileMode.Create);
                 * ExcelPackage.CopyStream(fs, st);
                 */
                //Save the digital signture
                Signature.Save(this);
            }
        }