Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        private Uri GetNewUri(Packaging.ZipPackage package, string sUri)
        {
            Uri uri;

            do
            {
                uri = new Uri(string.Format(sUri, _id++), UriKind.Relative);
            }while (package.PartExists(uri));
            return(uri);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        internal ExcelVbaProject(ExcelWorkbook wb)
        {
            _wb        = wb;
            _pck       = _wb._package.Package;
            References = new ExcelVbaReferenceCollection();
            Modules    = new ExcelVbaModuleCollection(this);
            var rel = _wb.Part.GetRelationshipsByType(schemaRelVba).FirstOrDefault();

            if (rel != null)
            {
                Uri  = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
                Part = _pck.GetPart(Uri);
                GetProject();
            }
            else
            {
                Lcid = 0;
                Part = null;
            }
        }
Ejemplo n.º 6
0
 private void ConstructNewFile(Stream stream, string password)
 {
     _stream = stream;
     if (File != null) File.Refresh();
     if (File != null && File.Exists)
     {
         if (password != null)
         {
             var encrHandler = new EncryptedPackageHandler();
             Encryption.IsEncrypted = true;
             Encryption.Password = password;
             _stream = encrHandler.DecryptPackage(File, Encryption);
             encrHandler = null;
         }
         else
         {
             ReadFile();
         }
         try
         {
             //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite);
             _package = new Packaging.ZipPackage(_stream);
         }
         catch (Exception ex)
        {
            if (password == null && CompoundDocument.IsStorageFile(File.FullName) == 0)
            {
                throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
            }
            else
            {
                throw;
            }
         }
     }
     else
     {
         //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite);
         _package = new Packaging.ZipPackage(_stream);
         CreateBlankWb();
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Closes the package.
 /// </summary>
 public void Dispose()
 {
     if(_package != null)
     {
         if (Stream != null && (Stream.CanRead || Stream.CanWrite))
         {
             Stream.Close();
         }
         _package.Close();
         ((IDisposable)_stream).Dispose();
         ((IDisposable)_workbook).Dispose();
         _package = null;
         _images = null;
         _file = null;
         _workbook = null;
         _stream = null;
         _workbook = null;
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Create a new instance of the ExcelPackage class based on a stream
        /// </summary>
        /// <param name="newStream">The stream object can be empty or contain a package. The stream must be Read/Write</param>
        /// <param name="Password">The password to decrypt the document</param>
        public ExcelPackage(Stream newStream, string Password)
        {
            if (!(newStream.CanRead && newStream.CanWrite))
            {
                throw new Exception("The stream must be read/write");
            }

            Init();
            if (newStream.Length > 0)
            {
                Load(newStream,Password);
            }
            else
            {
                _stream = newStream;
                //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite); TODO:Remove
                _package = new Packaging.ZipPackage(_stream);
                CreateBlankWb();
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="Password"></param>
        private void Load(Stream input, Stream output, string Password)
        {
            //Release some resources:
            if (this._package != null)
            {
                this._package.Close();
                this._package = null;
            }
            if (this._stream != null)
            {
                this._stream.Close();
                this._stream.Dispose();
                this._stream = null;
            }

            if (Password != null)
            {
                Stream encrStream = new MemoryStream();
                CopyStream(input, ref encrStream);
                EncryptedPackageHandler eph=new EncryptedPackageHandler();
                Encryption.Password = Password;
                this._stream = eph.DecryptPackage((MemoryStream)encrStream, Encryption);
            }
            else
            {
                this._stream = output;
                CopyStream(input, ref this._stream);
            }

            try
            {
                //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite);
                _package = new Packaging.ZipPackage(_stream);
            }
            catch (Exception ex)
            {
                EncryptedPackageHandler eph = new EncryptedPackageHandler();
                if (Password == null && CompoundDocument.IsStorageILockBytes(CompoundDocument.GetLockbyte((MemoryStream)_stream)) == 0)
                {
                    throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
                }
                else
                {
                    throw;
                }
            }

            //Clear the workbook so that it gets reinitialized next time
            this._workbook = null;
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Create a new file from a template
 /// </summary>
 /// <param name="template">An existing xlsx file to use as a template</param>
 /// <param name="password">The password to decrypt the package.</param>
 /// <returns></returns>
 private void CreateFromTemplate(FileInfo template, string password)
 {
     if (template != null) template.Refresh();
     if (template.Exists)
     {
         _stream = new MemoryStream();
         if (password != null)
         {
             Encryption.IsEncrypted = true;
             Encryption.Password = password;
             var encrHandler = new EncryptedPackageHandler();
             _stream = encrHandler.DecryptPackage(template, Encryption);
             encrHandler = null;
             //throw (new NotImplementedException("No support for Encrypted packages in this version"));
         }
         else
         {
             byte[] b = System.IO.File.ReadAllBytes(template.FullName);
             _stream.Write(b, 0, b.Length);
         }
         try
         {
             //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite);
             _package = new Packaging.ZipPackage(_stream);
         }
         catch (Exception ex)
         {
             if (password == null && CompoundDocument.IsStorageFile(template.FullName)==0)
             {
                 throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
             }
             else
             {
                 throw;
             }
         }
     }
     else
         throw new Exception("Passed invalid TemplatePath to Excel Template");
     //return newFile;
 }
Ejemplo n.º 11
0
		/// <summary>
		/// Closes the package.
		/// </summary>
		public void Dispose()
		{
            if(_package != null)
            {
                if (_isExternalStream==false && Stream != null && (Stream.CanRead || Stream.CanWrite))
                {
                    Stream.Close();
                }
                _package.Close();
                if(_isExternalStream==false) ((IDisposable)_stream).Dispose();
                if(_workbook != null)
                {
                    _workbook.Dispose();
                }
                _package = null;
                _images = null;
                _file = null;
                _workbook = null;
                _stream = null;
                _workbook = null;
            }
		}
Ejemplo n.º 12
0
        private void ConstructNewFile(string password)
        {
            var ms = new MemoryStream();
            if (_stream == null) _stream = new MemoryStream();
            if (File != null) File.Refresh();
            if (File != null && File.Exists)
            {
                if (password != null)
                {
#if !MONO
                    var encrHandler = new EncryptedPackageHandler();
                    Encryption.IsEncrypted = true;
                    Encryption.Password = password;
                    ms = encrHandler.DecryptPackage(File, Encryption);
                    encrHandler = null;
#endif
#if MONO
                    throw new NotImplementedException("No support for Encrypted packages in Mono");
#endif
                }
                else
                {
                    byte[] b = System.IO.File.ReadAllBytes(File.FullName);
                    ms.Write(b, 0, b.Length);
                }
                try
                {
                    //_package = Package.Open(_stream, FileMode.Open, FileAccess.ReadWrite);
                    _package = new Packaging.ZipPackage(ms);
                }
                catch (Exception ex)
               {
#if !MONO
                    if (password == null && CompoundDocument.IsStorageFile(File.FullName)==0)
                    {
                        throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
                    }
                    else
                    {
                        throw;
                    }
#endif
#if MONO
                    throw;
#endif
                }
            }
            else
            {
                //_package = Package.Open(_stream, FileMode.Create, FileAccess.ReadWrite);
                _package = new Packaging.ZipPackage(ms);
                CreateBlankWb();
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="Password"></param>
        private void Load(Stream input, Stream output, string Password)
        {
            //Release some resources:
            if (this._package != null)
            {
                this._package.Close();
                this._package = null;
            }
            if (this._stream != null)
            {
                this._stream.Close();
                this._stream.Dispose();
                this._stream = null;
            }
            _isExternalStream = true;
            if (input.Length == 0) // Template is blank, Construct new
            {
                _stream = output;
                ConstructNewFile(Password);
            }
            else
            {
                Stream ms;
                this._stream = output;
                if (Password != null)
                {
#if !MONO
                    Stream encrStream = new MemoryStream();
                    CopyStream(input, ref encrStream);
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    Encryption.Password = Password;
                    ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption);
#endif
#if MONO
                    throw new NotSupportedException("Encryption is not supported under Mono.");
#endif
                }
                else
                {
                    ms = new MemoryStream();
                    CopyStream(input, ref ms);
                }

                try
                {
                    //this._package = Package.Open(this._stream, FileMode.Open, FileAccess.ReadWrite);
                    _package = new Packaging.ZipPackage(ms);
                }
                catch (Exception ex)
                {
#if !MONO
                    EncryptedPackageHandler eph = new EncryptedPackageHandler();
                    if (Password == null && CompoundDocument.IsStorageILockBytes(CompoundDocument.GetLockbyte((MemoryStream)_stream)) == 0)
                    {
                        throw new Exception("Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password", ex);
                    }
                    else
                    {
                        throw;
                    }
#endif
#if MONO
                    throw;
#endif
                }
            }            
            //Clear the workbook so that it gets reinitialized next time
            this._workbook = null;
        }