Example #1
0
 /// <summary>
 /// Write one object to excel at current location and move to next location for next object
 /// </summary>
 /// <param name="obj"></param>
 public void Write(T obj)
 {
     // Calculate the entity range
     if (_entityRange == null)
     {     // No entity range calculated, this is the first entity
         if (_collectionRange == null)
         { // First time, calculate collection range
             if (_parentRange != null)
             {
                 _collectionRange = _parentRange.GetSubRange(_colCfg.Range);
             }
             else
             {
                 _collectionRange = _doc.GetRange(_colCfg.Range);
             }
             if (_collectionRange == null)
             {
                 throw new InvalidDataException(String.Format("Cannot find valid range for collection of {0}!",
                                                              typeof(T).FullName));
             }
         }
         EntityConfigElement entCfg = _colCfg.ItemTemplate.Entity;
         String entRef   = _collectionRange.Sheet.ExpandToSheetBound(entCfg.Range);
         String entRange = ExcelOpenXMLHelper.CalculateEntityRange(_collectionRange, entRef, 0,
                                                                   _colCfg.Orientation.Equals("vertical", StringComparison.OrdinalIgnoreCase));
         if (entRange == null)
         {
             throw new NoMoreEntityException(typeof(T));
         }
         _entityRange = _collectionRange.GetSubRange(entRange);
         if (_entityRange == null)
         {
             throw new NoMoreEntityException(typeof(T));
         }
     }
     // Write object to range
     _entityRange.WriteEntity(_colCfg.ItemTemplate.Entity, obj);
     // Move range to next
     if (_colCfg.Orientation.Equals("vertical", StringComparison.OrdinalIgnoreCase))
     {   // Move vertically
         int h = _entityRange.Height;
         _entityRange.Move(0, h);
     }
     else
     {   // Move horizontally
         int w = _entityRange.Width;
         _entityRange.Move(w, 0);
     }
 }
Example #2
0
        /// <summary>
        /// Read entity at specific index
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected T ReadCurrent()
        {
            // Get range of collection
            if (_collectionRange == null)
            {
                if (_parentRange != null)
                {
                    _collectionRange = _parentRange.GetSubRange(_colCfg.Range);
                }
                else
                {
                    _collectionRange = _doc.GetRange(_colCfg.Range);
                }
                if (_collectionRange == null)
                {
                    throw new InvalidDataException(String.Format("Cannot find valid range for collection of {0}!",
                                                                 typeof(T).FullName));
                }
                // For dynamic collection, it's possible to use a defined name to mark the end of co
            }
            // Calculate range of current entity
            if (_entityRange == null)
            {
                EntityConfigElement entCfg = _colCfg.ItemTemplate.Entity;
                String entRef   = _collectionRange.Sheet.ExpandToSheetBound(entCfg.Range);
                String entRange = ExcelOpenXMLHelper.CalculateEntityRange(_collectionRange, entRef, _currentIdx,
                                                                          _colCfg.Orientation.Equals("vertical", StringComparison.OrdinalIgnoreCase), _endBefore);
                if (entRange == null)
                {
                    throw new NoMoreEntityException(typeof(T));
                }
                _entityRange = _collectionRange.GetSubRange(entRange);
                if (_entityRange == null)
                {
                    throw new NoMoreEntityException(typeof(T));
                }
            }
            // Read data for entity
            var ent = _entityRange.ReadEntity <T>(_colCfg.ItemTemplate.Entity);

            if (ent == null)
            {
                throw new NoMoreEntityException(typeof(T));
            }
            return(ent);
        }