Пример #1
0
        Dictionary <int, ColumnInfo> GetColumns(ISheet sheet, TypeMapper typeMapper)
        {
            var columnsByIndex = typeMapper.ColumnsByIndex;

            SetColumnStyles(sheet, columnsByIndex);

            if (HeaderRow)
            {
                var columnsByName     = typeMapper.ColumnsByName;
                var headerRow         = sheet.GetRow(HeaderRowNumber);
                var hasColumnsByIndex = columnsByIndex.Any();

                if (headerRow == null)
                {
                    var j = 0;
                    headerRow = sheet.CreateRow(HeaderRowNumber);

                    if (!hasColumnsByIndex)
                    {
                        columnsByIndex = new Dictionary <int, ColumnInfo>();
                    }

                    foreach (var getter in columnsByName)
                    {
                        var columnIndex = !hasColumnsByIndex ? j : columnsByIndex.First(c => c.Value.Property == getter.Value.Property).Key;
                        var cell        = headerRow.GetCell(columnIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK);

                        if (!hasColumnsByIndex)
                        {
                            columnsByIndex[j] = getter.Value;
                        }

                        cell.SetCellValue(getter.Key);

                        j++;
                    }
                }
                else if (!hasColumnsByIndex)
                {
                    columnsByIndex = headerRow.Cells
                                     .Where(c => c.CellType == CellType.String && !string.IsNullOrWhiteSpace(c.StringCellValue))
                                     .Select(c => new { c.ColumnIndex, ColumnInfo = typeMapper.GetColumnByName(c.StringCellValue) })
                                     .Where(c => c.ColumnInfo != null)
                                     .ToDictionary(c => c.ColumnIndex, c => c.ColumnInfo);
                }
            }

            return(columnsByIndex);
        }
Пример #2
0
        Dictionary <int, ColumnInfo> GetColumns(ISheet sheet, TypeMapper typeMapper)
        {
            Dictionary <int, ColumnInfo> columnsByIndex;

            if (HeaderRow)
            {
                var columnsByName = typeMapper.ColumnsByName;
                var headerRow     = sheet.GetRow(0);

                if (headerRow == null)
                {
                    var j = 0;
                    columnsByIndex = new Dictionary <int, ColumnInfo>();
                    headerRow      = sheet.CreateRow(0);

                    foreach (var getter in columnsByName)
                    {
                        var cell = headerRow.GetCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
                        cell.SetCellValue(getter.Key);
                        columnsByIndex[j] = getter.Value;
                        j++;
                    }
                }
                else
                {
                    columnsByIndex = headerRow.Cells
                                     .Where(c => !string.IsNullOrWhiteSpace(c.StringCellValue))
                                     .Select(c => new { ColumnIndex = c.ColumnIndex, ColumnInfo = HeaderRow ? typeMapper.GetColumnByName(c.StringCellValue) : typeMapper.GetColumnByIndex(c.ColumnIndex) })
                                     .Where(c => c.ColumnInfo != null)
                                     .ToDictionary(c => c.ColumnIndex, c => c.ColumnInfo);
                }
            }
            else
            {
                columnsByIndex = typeMapper.ColumnsByIndex;
            }

            return(columnsByIndex);
        }