Ejemplo n.º 1
0
        private void AddSupportedCell(ExcelColumnDescriptor.EnumColumnType cellType, Func <object, string> converter, CellValues cellValueType)
        {
            if (_StringCellValueConverter.ContainsKey(cellType))
            {
                throw new InvalidOperationException($"Unable to add {cellType}: item already registered");
            }

            _StringCellValueConverter.Add(cellType, converter);
            _CellTypeConverter.Add(cellType, cellValueType);
        }
Ejemplo n.º 2
0
        private Cell CreateTypedRowCell(int rowNumber, object cellValue, ExcelColumnDescriptor.EnumColumnType colType, int columnIndexZeroBased, uint style = 1)
        {
            try
            {
                // get the name of column
                var columnName = GetColumnNameByIndex(columnIndexZeroBased);
                // craete cell
                var cell = this._CellConverter.CreateNewCell(cellValue, colType, columnName, rowNumber);
                // set proper style
                cell.StyleIndex = style;

                return(cell);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Unable to create cell of type {colType} in row {rowNumber}: {ex.Message} [Value: {cellValue}]", ex);
            }
        }
Ejemplo n.º 3
0
        public Cell CreateNewCell(object value, ExcelColumnDescriptor.EnumColumnType cellType, string column, int rowNumber)
        {
            if (String.IsNullOrEmpty(column))
            {
                throw new ArgumentNullException(nameof(column));
            }
            if (rowNumber == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(rowNumber), "The row number must to be greater or equals to 1");
            }

            if (cellType == ExcelColumnDescriptor.EnumColumnType.Formula)
            {
                Cell cell = new Cell()
                {
                    CellReference = $"{column}{rowNumber}"
                };
                CellFormula cellformula = new CellFormula(this._StringCellValueConverter[cellType].Invoke(value))
                {
                    CalculateCell = true,
                };
                cell.Append(cellformula);
                return(cell);
            }
            if (_StringCellValueConverter.ContainsKey(cellType))
            {
                return(new Cell()
                {
                    CellValue = new CellValue(this._StringCellValueConverter[cellType].Invoke(value)),
                    DataType = new EnumValue <CellValues>(_CellTypeConverter[cellType])
                });
            }
            else
            {
                throw new NotImplementedException("unknown cell type " + cellType);
            }
        }