Example #1
0
 public Style Parse(string s)
 {
     if (s != null && s.Length > 0)
     {
         Clear();
         styleStr = s.ToLower();
         try
         {
             Style style = new Style();
             AddParams();
             foreach (BaseStyleBuilder sb in styleBuilders.Values)
             {
                 if (!sb.IsEmpty)
                 {
                     IBaseStyle st = sb.CreateStyle();
                     if (st != null)
                     {
                         style.AddStyle(st);
                     }
                 }
             }
             return(style.Count == 0 ? null : style);
         }
         catch//(Exception ex)
         {
             //	!!!			Log.Exception(ex);
         }
     }
     return(null);
 }
        public ISheet CreateSheet <T>(Type dateType, string sheetName, SheetTitle title, List <T> data, List <string> filterColumn)
            where T : SheetRow
        {
            var sheet = string.IsNullOrWhiteSpace(sheetName) ? _workBook.CreateSheet() : _workBook.CreateSheet(sheetName);

            if (data == null)
            {
                return(sheet);
            }

            var columnProperties = GetColumnProperties(dateType, filterColumn);

            int rowIndex = 0;

            if (title != null)
            {
                this.CreateTitle(title, sheet, columnProperties.Count);
                rowIndex++;
            }

            this.CreateHeader(sheet, columnProperties, rowIndex++);

            var cacheStyles = new Dictionary <string, ICellStyle>();

            foreach (var item in data)
            {
                var columnId = 0;
                var dataRow  = sheet.CreateRow(rowIndex);
                item.ExportRowIndex = rowIndex;
                rowIndex++;
                foreach (var property in columnProperties)
                {
                    var cell  = dataRow.CreateCell(columnId++);
                    var value = property.PropertyInfo.GetValue(item);
                    if (value == null)
                    {
                        cell.SetCellValue(string.Empty);
                        continue;
                    }

                    string valueStr;
                    if (property.PropertyInfo.PropertyType == typeof(DateTime) || property.PropertyInfo.PropertyType == typeof(DateTime?))
                    {
                        var format = string.IsNullOrWhiteSpace(property.StringFormat) ? DefaultDateFormat : property.StringFormat;
                        valueStr = ((DateTime)property.PropertyInfo.GetValue(item)).ToString(format);
                    }
                    else
                    {
                        valueStr = property.PropertyInfo.GetValue(item).ToString();
                    }

                    cell.SetCellValue(valueStr);
                    IBaseStyle cellStyle = null;
                    if ((item.CellStyles?.Any() ?? false) && item.CellStyles.ContainsKey(property.PropertyInfo.Name))
                    {
                        cellStyle = item.CellStyles[property.PropertyInfo.Name];
                    }
                    SetCellStyle(cell, cacheStyles, property, cellStyle);
                }
            }

            this.RowMerged(data, sheet, columnProperties);

            this.SetColumnWidth(sheet, columnProperties);

            return(sheet);
        }
        private void SetCellStyle(ICell cell, Dictionary <string, ICellStyle> cacheStyles, ExportColumnProperty property, IBaseStyle cellStyle = null)
        {
            var style = cellStyle ?? property.ColumnStyle;

            if (cacheStyles.ContainsKey(style.ToString()))
            {
                cell.CellStyle = cacheStyles[style.ToString()];
                return;
            }
            var fontStyle = _workBook.CreateCellStyle();
            var font      = _workBook.CreateFont();

            font.IsBold             = style.IsBold;
            font.FontName           = style.FontName;
            font.FontHeightInPoints = style.FontSize;
            font.Color = style.FontColor;
            fontStyle.SetFont(font);
            fontStyle.VerticalAlignment = (VerticalAlignment)style.VerticalAlign;
            fontStyle.Alignment         = (HorizontalAlignment)style.HorizontalAlign;
            if (style.FillForegroundColor != 0)
            {
                fontStyle.FillPattern         = FillPattern.SolidForeground;
                fontStyle.FillForegroundColor = style.FillForegroundColor;
            }
            fontStyle.WrapText = style.WrapText;
            cell.CellStyle     = fontStyle;
            cacheStyles.Add(style.ToString(), fontStyle);
        }