Ejemplo n.º 1
0
        private Paragraph AppendParagraph(Cell cell)
        {
            var para = cell.Paragraphs.FirstOrDefault();

            if (para == null)
            {
                var formatting = GetFormatting();

                para = cell.InsertParagraph(string.Empty, false, formatting);

                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;
                }
            }
            else
            {
                SetParagraphFormat(para);
            }

            if (_style.HasBgColor())
            {
                var p       = new HSSFPalette(new PaletteRecord());
                var c       = p.GetColor(_style.BgColor);
                var triplet = c.GetTriplet();
                cell.FillColor = Color.FromArgb(triplet[0], triplet[1], triplet[2]);
            }

            return(para);
        }
Ejemplo n.º 2
0
        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;
            }
        }