private void ReadSheetGlobals(XlsxWorksheet sheet)
        {
            _sheetStream = _zipWorker.GetWorksheetStream(sheet.Path);

            if (null == _sheetStream)
            {
                return;
            }

            _xmlReader = XmlReader.Create(_sheetStream);

            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == XlsxWorksheet.N_dimension)
                {
                    var dimValue = _xmlReader.GetAttribute(XlsxWorksheet.A_ref);

                    if (dimValue.IndexOf(':') > 0)
                    {
                        sheet.Dimension = new XlsxDimension(dimValue);
                    }
                    else
                    {
                        _xmlReader.Close();
                        _sheetStream.Close();
                    }

                    break;
                }
            }
        }
Esempio n. 2
0
        private async Task ReadSheetGlobalsAsync(XlsxWorksheet sheet)
        {
            if (_xmlReader != null)
            {
                _xmlReader.Dispose();
            }
            if (_sheetStream != null)
            {
                _sheetStream.Dispose();
            }

            _sheetStream = await _zipWorker.GetWorksheetStream(sheet.Path);

            if (null == _sheetStream)
            {
                return;
            }

            _xmlReader = XmlReader.Create(_sheetStream);

            //count rows and cols in case there is no dimension elements
            int rows = 0;
            int cols = 0;

            _namespaceUri = null;
            int biggestColumn = 0;         //used when no col elements and no dimension

            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_worksheet)
                {
                    //grab the namespaceuri from the worksheet element
                    _namespaceUri = _xmlReader.NamespaceURI;
                }

                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_dimension)
                {
                    string dimValue = _xmlReader.GetAttribute(XlsxWorksheet.A_ref);

                    sheet.Dimension = new XlsxDimension(dimValue);
                    break;
                }

                //removed: Do not use col to work out number of columns as this is really for defining formatting, so may not contain all columns
                //if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_col)
                //    cols++;

                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_row)
                {
                    rows++;
                }

                //check cells so we can find size of sheet if can't work it out from dimension or col elements (dimension should have been set before the cells if it was available)
                //ditto for cols
                if (sheet.Dimension == null && cols == 0 && _xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_c)
                {
                    var refAttribute = _xmlReader.GetAttribute(XlsxWorksheet.A_r);

                    if (refAttribute != null)
                    {
                        var thisRef = ReferenceHelper.ReferenceToColumnAndRow(refAttribute);
                        if (thisRef[1] > biggestColumn)
                        {
                            biggestColumn = thisRef[1];
                        }
                    }
                }
            }


            //if we didn't get a dimension element then use the calculated rows/cols to create it
            if (sheet.Dimension == null)
            {
                if (cols == 0)
                {
                    cols = biggestColumn;
                }

                if (rows == 0 || cols == 0)
                {
                    sheet.IsEmpty = true;
                    return;
                }

                sheet.Dimension = new XlsxDimension(rows, cols);

                //we need to reset our position to sheet data
                _xmlReader.Dispose();
                _sheetStream.Dispose();
                _sheetStream = await _zipWorker.GetWorksheetStream(sheet.Path);

                _xmlReader = XmlReader.Create(_sheetStream);
            }

            //read up to the sheetData element. if this element is empty then there aren't any rows and we need to null out dimension

            _xmlReader.ReadToFollowing(XlsxWorksheet.N_sheetData, _namespaceUri);
            if (_xmlReader.IsEmptyElement)
            {
                sheet.IsEmpty = true;
            }
        }