コード例 #1
0
        /// <summary>
        /// Creates a new instance of the ExcelWorksheets class.
        /// For internal use only!
        /// </summary>
        /// <param name="xlPackage"></param>
        protected internal ExcelWorksheets(ExcelPackage xlPackage)
        {
            _xlPackage = xlPackage;
            //  Create a NamespaceManager to handle the default namespace,
            //  and create a prefix for the default namespace:
            NameTable nt = new NameTable();

            _nsManager = new XmlNamespaceManager(nt);
            _nsManager.AddNamespace("d", ExcelPackage.schemaMain);
            _nsManager.AddNamespace("r", ExcelPackage.schemaRelationships);

            // obtain container node for all worksheets
            _worksheetsNode = _xlPackage.Workbook.WorkbookXml.SelectSingleNode("//d:sheets", _nsManager);
            if (_worksheetsNode == null)
            {
                // create new node as it did not exist
                _worksheetsNode = _xlPackage.Workbook.WorkbookXml.CreateElement("sheets", ExcelPackage.schemaMain);
                _xlPackage.Workbook.WorkbookXml.DocumentElement.AppendChild(_worksheetsNode);
            }

            _worksheets = new List <ExcelWorksheet>();
            foreach (XmlNode sheetNode in _worksheetsNode.ChildNodes)
            {
                string name = sheetNode.Attributes["name"].Value;
                //  Get the relationship id attribute:
                string relId   = sheetNode.Attributes["r:id"].Value;
                int    sheetID = Convert.ToInt32(sheetNode.Attributes["sheetId"].Value);
                //if (sheetID != count)
                //{
                //  // renumber the sheets as they are in an odd order
                //  sheetID = count;
                //  sheetNode.Attributes["sheetId"].Value = sheetID.ToString();
                //}
                // get hidden attribute (if present)
                bool    hidden = false;
                XmlNode attr   = sheetNode.Attributes["hidden"];
                if (attr != null)
                {
                    hidden = Convert.ToBoolean(attr.Value);
                }

                //string type = "";
                //attr = sheetNode.Attributes["type"];
                //if (attr != null)
                //  type = attr.Value;

                PackageRelationship sheetRelation = _xlPackage.Workbook.Part.GetRelationship(relId);
                Uri uriWorksheet = PackUriHelper.ResolvePartUri(_xlPackage.Workbook.WorkbookUri, sheetRelation.TargetUri);

                // add worksheet to our collection
                _worksheets.Add(new ExcelWorksheet(_xlPackage, relId, name, uriWorksheet, sheetID, hidden));
            }

            _definedNames = new ExcelDefinedNames(this, _xlPackage.Workbook.WorkbookXml);
        }
コード例 #2
0
        /// <summary>
        /// Creates a new instance of the ExcelWorksheets class.
        /// For internal use only!
        /// </summary>
        /// <param name="xlPackage"></param>
        protected internal ExcelWorksheets(ExcelPackage xlPackage)
        {
            _xlPackage = xlPackage;
            //  Create a NamespaceManager to handle the default namespace,
            //  and create a prefix for the default namespace:
            NameTable nt = new NameTable();
            _nsManager = new XmlNamespaceManager(nt);
            _nsManager.AddNamespace("d", ExcelPackage.schemaMain);
            _nsManager.AddNamespace("r", ExcelPackage.schemaRelationships);

            // obtain container node for all worksheets
            _worksheetsNode = _xlPackage.Workbook.WorkbookXml.SelectSingleNode("//d:sheets", _nsManager);
            if (_worksheetsNode == null)
            {
                // create new node as it did not exist
                _worksheetsNode = _xlPackage.Workbook.WorkbookXml.CreateElement("sheets", ExcelPackage.schemaMain);
                _xlPackage.Workbook.WorkbookXml.DocumentElement.AppendChild(_worksheetsNode);
            }

            _worksheets = new List<ExcelWorksheet>();
            foreach (XmlNode sheetNode in _worksheetsNode.ChildNodes)
            {
                string name = sheetNode.Attributes["name"].Value;
                //  Get the relationship id attribute:
                string relId = sheetNode.Attributes["r:id"].Value;
                int sheetID = Convert.ToInt32(sheetNode.Attributes["sheetId"].Value);
                //if (sheetID != count)
                //{
                //  // renumber the sheets as they are in an odd order
                //  sheetID = count;
                //  sheetNode.Attributes["sheetId"].Value = sheetID.ToString();
                //}
                // get hidden attribute (if present)
                bool hidden = false;
                XmlNode attr = sheetNode.Attributes["hidden"];
                if (attr != null)
                    hidden = Convert.ToBoolean(attr.Value);

                //string type = "";
                //attr = sheetNode.Attributes["type"];
                //if (attr != null)
                //  type = attr.Value;

                PackageRelationship sheetRelation = _xlPackage.Workbook.Part.GetRelationship(relId);
                Uri uriWorksheet = PackUriHelper.ResolvePartUri(_xlPackage.Workbook.WorkbookUri, sheetRelation.TargetUri);

                // add worksheet to our collection
                _worksheets.Add(new ExcelWorksheet(_xlPackage, relId, name, uriWorksheet, sheetID, hidden));
            }

            _definedNames = new ExcelDefinedNames(this, _xlPackage.Workbook.WorkbookXml);
        }