private void WriteMember <T>(T value, IExcelMemberOptions options, DisplayType display, bool isHeaderRow = false) { var cursor = Cursor; var lastMemberColumn = GetLastMemberPosition(cursor.CurrentColumn, options.ColSpan); var lastMemberRow = GetLastMemberPosition(cursor.CurrentRow, options.RowSpan); var range = Worksheet.Cells[cursor.CurrentRow, cursor.CurrentColumn, lastMemberRow, lastMemberColumn]; ApplyStyle(options, isHeaderRow, range); range.Value = value; range.Merge = options.Merge; if (!string.IsNullOrWhiteSpace(options.FormatNumber)) { range.Style.Numberformat.Format = options.FormatNumber; } if ((value is DateTime || value is DateTimeOffset) && IsDefaultFormatNumber(range) && DateTimeFormatInfo.CurrentInfo != null) { range.Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern; } if (options.HAlign.HasValue) { range.Style.HorizontalAlignment = (ExcelHorizontalAlignment)options.HAlign; } if (options.VAlign.HasValue) { range.Style.VerticalAlignment = (ExcelVerticalAlignment)options.VAlign; } if (options.BackgroundColor.HasValue) { range.Style.Fill.BackgroundColor.SetColor(options.BackgroundColor.Value); } if (options.HasAlternateBackgroundColor && options.AlternateBackgroundColors != null && options.AlternateBackgroundColors.Length > 0 && !isHeaderRow) { range.Style.Fill.BackgroundColor.SetColor(options.AlternateBackgroundColors[(cursor.CurrentRow + 1) % options.AlternateBackgroundColors.Length]); } if (display == DisplayType.Block) { cursor.CurrentRow = lastMemberRow + 1; } else { cursor.CurrentColumn = lastMemberColumn + 1; } }
private void ApplyStyle(IExcelMemberOptions options, bool isHeaderRow, ExcelRange cells) { string styleName; ExcelStyle style; if (isHeaderRow) { styleName = options.HeaderStyleName; style = options.HeaderStyle; } else { styleName = options.StyleName; style = options.Style; } ExcelStyleApplier.ApplyStyle(styleName, style, cells); }
private static void ApplyStylesForRange(ExcelRange range, IExcelMemberOptions options) { if (options.HAlign.HasValue && range.Style.HorizontalAlignment != (ExcelHorizontalAlignment)options.HAlign) { range.Style.HorizontalAlignment = (ExcelHorizontalAlignment)options.HAlign; } if (options.VAlign.HasValue && range.Style.VerticalAlignment != (ExcelVerticalAlignment)options.VAlign) { range.Style.VerticalAlignment = (ExcelVerticalAlignment)options.VAlign; } if (options.BackgroundColor.HasValue && range.Style.Fill.BackgroundColor.Rgb != options.BackgroundColor.Value.ToArgb().ToString("X")) { range.Style.Fill.BackgroundColor.SetColor(options.BackgroundColor.Value); } if (options.IsStyleSet && range.StyleName != options.StyleName) { range.StyleName = options.StyleName; } }
public void WriteValue <T>(T value, IExcelMemberOptions options, DisplayType display) { WriteMember(value, options, display); }