private void SetParagraphFormat(Paragraph para) { if (para != null) { switch (_style.HAlign) { case HAlignment.Center: para.Alignment = Alignment.center; break; case HAlignment.FullWidth: para.Alignment = Alignment.both; break; case HAlignment.Left: para.Alignment = Alignment.left; break; case HAlignment.Right: para.Alignment = Alignment.right; break; } if (_style.HasFontStyle() && _style.FontStyle.HasFlag(FontStyle.Bold)) { para.Bold(); } if (_style.HasFontStyle() && _style.FontStyle.HasFlag(FontStyle.Italic)) { para.Italic(); } para.FontSize(12 + (_style.HasFontDSize() ? _style.FontDSize : 0)); if (_style.HasFontName()) { para.Font(new FontFamily(_style.FontName)); } if (_style.HasFontColor()) { var p = new HSSFPalette(new PaletteRecord()); var c = p.GetColor(_style.FontColor); var triplet = c.GetTriplet(); para.Color(Color.FromArgb(triplet[0], triplet[1], triplet[2])); } } }
private Formatting GetFormatting() { var result = (from f in _formattings where f.Key.CompareTo(_style) == 0 select f.Value).FirstOrDefault(); if (result == null) { result = new Formatting { Bold = _style.HasFontStyle() && _style.FontStyle.HasFlag(FontStyle.Bold), Italic = _style.HasFontStyle() && _style.FontStyle.HasFlag(FontStyle.Italic), Size = 12 + (_style.HasFontDSize() ? _style.FontDSize : 0) }; /*if (_style.HasFontColor()) * result.FontColor = (Color) _style.FontColor;*/ _formattings.Add(new ContentStyle(_style), result); } return(result); }
private void AssignStyle(CellStyle cellStyle, ContentStyle style) { if (style.HasFontName() || style.HasFontStyle() || style.HasFontDSize() || style.HasFontColor()) { var font = GetFont(style); //Workbook.CreateFont(); // cellStyle.GetFont(Workbook); if (style.HasFontName()) { font.FontName = style.FontName; } if (style.HasFontDSize()) { font.FontHeightInPoints += style.FontDSize; } if (style.HasFontStyle()) { if (style.FontStyle.HasFlag(FontStyle.Bold)) { font.Boldweight = (short)FontBoldWeight.BOLD; } else { font.Boldweight = (short)FontBoldWeight.NORMAL; } font.IsItalic = style.FontStyle.HasFlag(FontStyle.Italic); font.Underline = style.FontStyle.HasFlag(FontStyle.Underline) ? (byte)FontUnderlineType.SINGLE : (byte)FontUnderlineType.NONE; } if (style.HasFontColor()) { font.Color = style.FontColor; } cellStyle.SetFont(font); } if (style.HasBgColor()) { cellStyle.FillForegroundColor = style.BgColor; cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND; } switch (style.HAlign) { case HAlignment.Left: cellStyle.Alignment = HorizontalAlignment.LEFT; break; case HAlignment.Right: cellStyle.Alignment = HorizontalAlignment.RIGHT; break; case HAlignment.Center: cellStyle.Alignment = HorizontalAlignment.CENTER; break; } switch (style.VAlign) { case VAlignment.Top: cellStyle.VerticalAlignment = VerticalAlignment.TOP; break; case VAlignment.Bottom: cellStyle.VerticalAlignment = VerticalAlignment.BOTTOM; break; case VAlignment.Middle: cellStyle.VerticalAlignment = VerticalAlignment.CENTER; break; } if (style.Borders.HasFlag(TableCellBorder.Top) /* ?? false*/) { cellStyle.BorderTop = CellBorderType.THIN; cellStyle.TopBorderColor = style.BorderColor; //HSSFColor.BLACK.index; } else { cellStyle.BorderTop = CellBorderType.NONE; } if (style.Borders.HasFlag(TableCellBorder.Left)) { cellStyle.BorderLeft = CellBorderType.THIN; cellStyle.LeftBorderColor = style.BorderColor; //HSSFColor.BLACK.index; } else { cellStyle.BorderLeft = CellBorderType.NONE; } if (style.Borders.HasFlag(TableCellBorder.Right)) { cellStyle.BorderRight = CellBorderType.THIN; cellStyle.RightBorderColor = style.BorderColor; //HSSFColor.BLACK.index; } else { cellStyle.BorderRight = CellBorderType.NONE; } if (style.Borders.HasFlag(TableCellBorder.Bottom)) { cellStyle.BorderBottom = CellBorderType.THIN; cellStyle.BottomBorderColor = style.BorderColor; //HSSFColor.BLACK.index; } else { cellStyle.BorderBottom = CellBorderType.NONE; } if (style.WrapText ?? false) { cellStyle.WrapText = (bool)style.WrapText; } }
private Font GetFont(ContentStyle style) { var curFont = Workbook.GetFontAt(0); for (short i = 1; i < Workbook.NumberOfFonts; i++) { var font = Workbook.GetFontAt(i); if (style.HasFontName()) { if (!String.Equals(font.FontName, style.FontName, StringComparison.OrdinalIgnoreCase)) { continue; } } else if (!String.Equals(font.FontName, curFont.FontName, StringComparison.OrdinalIgnoreCase)) { continue; } if (style.HasFontStyle()) { if (style.FontStyle.HasFlag(FontStyle.Italic) && !font.IsItalic) { continue; } if (style.FontStyle.HasFlag(FontStyle.Bold) && font.Boldweight != (short)FontBoldWeight.BOLD) { continue; } if (style.FontStyle.HasFlag(FontStyle.Underline) && font.Underline != (byte)FontUnderlineType.SINGLE) { continue; } if (style.FontStyle == FontStyle.Regular && (font.IsItalic || font.Boldweight != (short)FontBoldWeight.NORMAL || font.Underline != (byte)FontUnderlineType.NONE)) { continue; } } if (style.FontDSize == 0 && curFont.FontHeightInPoints != font.FontHeightInPoints) { continue; } if (style.FontDSize != 0) { var fs = curFont.FontHeightInPoints + style.FontDSize; if (font.FontHeightInPoints != fs) { continue; } } if (style.HasFontColor() && style.FontColor != font.Color) { continue; } return(font); } return(Workbook.CreateFont()); }