Ejemplo n.º 1
0
        /// <summary>
        /// Get a SheetReader instance representing the worksheet at the given zero-based index
        /// </summary>
        /// <param name="sheetNumber">The zero-based index of the worksheet</param>
        public SheetReader this[int sheetNumber]
        {
            get
            {
                if (_sheetReadersByInteger == null)
                {
                    _sheetReadersByInteger = new Dictionary <int, SheetReader>();
                }

                if (_sheetReadersByInteger.ContainsKey(sheetNumber))
                {
                    return(_sheetReadersByInteger[sheetNumber]);
                }

                if (_zippedXlsxFile == null)
                {
                    _zippedXlsxFile = new ZippedXlsxFile(_filePath);
                }

                var sheetReader = new SheetReader(_zippedXlsxFile.GetWorksheetStream(sheetNumber),
                                                  _zippedXlsxFile.SharedStringsStream, _zippedXlsxFile.IsDateTimeStream, ReadNextBehaviour);
                _sheetReadersByInteger.Add(sheetNumber, sheetReader);
                return(sheetReader);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get a SheetReader instance representing the worksheet with the given name
        /// </summary>
        /// <param name="sheetName">The name of the worksheet</param>
        /// <exception cref="IndexOutOfRangeException">Will throw if the worksheet does not exist</exception>
        public SheetReader this[string sheetName]
        {
            get
            {
                if (_sheetnameLookup == null)
                {
                    _sheetnameLookup = new Dictionary <string, int>();
                }

                if (_zippedXlsxFile == null)
                {
                    _zippedXlsxFile = new ZippedXlsxFile(_filePath);
                }

                if (_sheetReadersByInteger == null)
                {
                    _sheetReadersByInteger = new Dictionary <int, SheetReader>();
                }

                int?sheetNumber;
                if (_sheetnameLookup.ContainsKey(sheetName))
                {
                    sheetNumber = _sheetnameLookup[sheetName];
                }
                else
                {
                    sheetNumber = ReadSheetNumberFromXml(sheetName);
                }

                if (!sheetNumber.HasValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(sheetName), $"Sheet with name '{sheetName}' was not found in the workbook");
                }

                if (_sheetReadersByInteger.ContainsKey(sheetNumber.Value))
                {
                    var existingSheet = _sheetReadersByInteger[sheetNumber.Value];
                    return(existingSheet);
                }

                var sheetReader = new SheetReader(_zippedXlsxFile.GetWorksheetStream(sheetNumber.Value),
                                                  _zippedXlsxFile.SharedStringsStream, _zippedXlsxFile.IsDateTimeStream, ReadNextBehaviour);
                _sheetReadersByInteger.Add(sheetNumber.Value, sheetReader);
                return(sheetReader);
            }
        }