Beispiel #1
0
        /// <summary>
        /// Tao font
        /// </summary>
        /// <param name="wk">WorkBook</param>
        /// <param name="Boldweight">Độ đậm - FontBoldWeight</param>
        /// <param name="color">Màu chữ - FontColor</param>
        /// <param name="fontsize">cỡ chữ</param>
        /// <param name="FontName">kiểu chữ</param>
        /// <param name="IsItalic">in nghiêng</param>
        /// <param name="Underline">ngạch chân - FontUnderlineType</param>
        /// <param name="TypeOffset">Vi tri - FontSuperScript</param>
        /// <returns></returns>
        /// <modified>
        /// Author      Date        comment
        /// anhhn      15/07/2012  Tạo mới
        /// </modified>
        public static IFont CreateFont(this IWorkbook wk, short Boldweight, short color, short fontsize, string FontName, bool IsItalic,
                                       FontUnderlineType Underline, FontSuperScript TypeOffset)
        {
            IFont result = wk.CreateFont();

            result.Boldweight         = Boldweight;
            result.Color              = color;
            result.FontHeightInPoints = fontsize;
            result.FontName           = FontName;
            result.IsItalic           = IsItalic;
            result.Underline          = Underline;
            result.TypeOffset         = TypeOffset;
            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// 创建一个新的单元格样式对象
 /// </summary>
 public ExcelCellStyle()
 {  // ICellStyle e;
     _dataFormart        = string.Empty;
     _fontSize           = 9;
     _fontBold           = FontBoldWeight.None;
     _fontName           = "Arial";
     _alignment          = HorizontalAlignment.Left;
     _verticalAlignment  = VerticalAlignment.Center;
     _borderBottom       = BorderStyle.None;
     _borderLeft         = BorderStyle.None;
     _borderRight        = BorderStyle.None;
     _borderTop          = BorderStyle.None;
     _styleValueIsChange = true;
     _cellStyle          = null;
     _warpText           = false;
     _underline          = FontUnderlineType.None;
     _fontColor          = NPOI.HSSF.Util.HSSFColor.Black.Index;
     _isItalic           = false;
     _isStrikeout        = false;
     _fontTypeOffset     = 0;
 }
Beispiel #3
0
 /**
  * Finds a font that matches the one with the supplied attributes
  */
 public IFont FindFont(short boldWeight, short color, short fontHeight, String name, bool italic, bool strikeout,FontSuperScript typeOffset, FontUnderlineType underline)
 {
     return stylesSource.FindFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, underline);
 }
Beispiel #4
0
 /**
  * Finds a font that matches the one with the supplied attributes
  */
 public XSSFFont FindFont(short boldWeight, short color, short fontHeight, String name, bool italic, bool strikeout, FontSuperScript typeOffset, FontUnderlineType underline)
 {
     foreach (XSSFFont font in fonts)
     {
         if ((font.Boldweight == boldWeight) &&
             font.Color == color &&
             font.FontHeight == fontHeight &&
             font.FontName.Equals(name) &&
             font.IsItalic == italic &&
             font.IsStrikeout == strikeout &&
             font.TypeOffset == typeOffset &&
             font.Underline == underline)
         {
             return(font);
         }
     }
     return(null);
 }
Beispiel #5
0
 /**
  * Finds a font that matches the one with the supplied attributes
  */
 public XSSFFont FindFont(short boldWeight, short color, short fontHeight, String name, bool italic, bool strikeout, FontSuperScript typeOffset,FontUnderlineType underline)
 {
     foreach (XSSFFont font in fonts)
     {
         if ((font.Boldweight == boldWeight)
                 && font.Color == color
                 && font.FontHeight == fontHeight
                 && font.FontName.Equals(name)
                 && font.IsItalic == italic
                 && font.IsStrikeout == strikeout
                 && font.TypeOffset == typeOffset
                 && font.Underline == underline)
         {
             return font;
         }
     }
     return null;
 }
Beispiel #6
0
 public IFont FindFont(short boldWeight, short color, short fontHeight, string name, bool italic, bool strikeout, FontSuperScript typeOffset, FontUnderlineType underline)
 {
     return(XssfWorkbook.FindFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, underline));
 }
Beispiel #7
0
 /// <summary>
 /// 获取字体样式
 /// </summary>
 /// <param name="hssfworkbook">Excel操作类</param>
 /// <param name="fontfamily">字体名</param>
 /// <param name="fontcolor">字体颜色</param>
 /// <param name="fontsize">字体大小</param>
 /// <param name="bold">是否加粗</param>
 /// <param name="typeOffset">上下标</param>
 /// <returns></returns>
 public static IFont GetFontStyle(XSSFWorkbook hssfworkbook, string fontfamily, XSSFColor fontcolor, int fontsize, FontBoldWeight bold = FontBoldWeight.Normal, FontSuperScript typeOffset = FontSuperScript.None)
 {
     XSSFFont xfont = hssfworkbook.CreateFont() as XSSFFont;
     if (string.IsNullOrEmpty(fontfamily))
     {
         xfont.FontName = fontfamily;
     }
     if (fontcolor != null)
     {
         xfont.SetColor(fontcolor);
     }
     xfont.IsItalic = false;
     xfont.FontHeightInPoints = (short)fontsize;
     xfont.Boldweight = (short)bold;
     xfont.TypeOffset = typeOffset;
     return xfont;
 }
Beispiel #8
0
 /// <summary>
 /// Sets superscript type.
 /// </summary>
 /// <param name="cell">The cell.</param>
 /// <param name="superScript">The super script.</param>
 /// <returns>A <seealso cref="FluentCell"/>, for further styling.</returns>
 public static FluentCell SuperScript(this ICell cell, FontSuperScript superScript)
 {
     return(new FluentCell(cell).SuperScript(superScript));
 }
Beispiel #9
0
        /// <summary>
        /// 获取字体样式
        /// </summary>
        /// <param name="hssfworkbook">Excel操作类</param>
        /// <param name="fontfamily">字体名</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="fontsize">字体大小</param>
        /// <param name="bold">是否加粗</param>
        /// <param name="typeOffset">上下标</param>
        /// <returns></returns>
        public static IFont GetFontStyle(XSSFWorkbook hssfworkbook, string fontfamily, XSSFColor fontcolor, int fontsize, FontBoldWeight bold = FontBoldWeight.Normal, FontSuperScript typeOffset = FontSuperScript.None)
        {
            XSSFFont xfont = hssfworkbook.CreateFont() as XSSFFont;

            if (string.IsNullOrEmpty(fontfamily))
            {
                xfont.FontName = fontfamily;
            }
            if (fontcolor != null)
            {
                xfont.SetColor(fontcolor);
            }
            xfont.IsItalic           = false;
            xfont.FontHeightInPoints = (short)fontsize;
            xfont.Boldweight         = (short)bold;
            xfont.TypeOffset         = typeOffset;
            return(xfont);
        }
Beispiel #10
0
        //public NPOI.SS.UserModel.Font FindFont(NPOI.SS.UserModel.FontBoldWeight boldWeight, short color, short fontHeight,
        //                 String name, bool italic, bool strikeout,
        //                 NPOI.SS.UserModel.FontSuperScript typeOffset, NPOI.SS.UserModel.FontUnderlineType Underline)
        //{
        //    return this.FindFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, Underline);
        //}

        /// <summary>
        /// Finds a font that matches the one with the supplied attributes
        /// </summary>
        /// <param name="boldWeight">The bold weight.</param>
        /// <param name="color">The color.</param>
        /// <param name="fontHeight">Height of the font.</param>
        /// <param name="name">The name.</param>
        /// <param name="italic">if set to <c>true</c> [italic].</param>
        /// <param name="strikeout">if set to <c>true</c> [strikeout].</param>
        /// <param name="typeOffset">The type offset.</param>
        /// <param name="Underline">The underline.</param>
        /// <returns></returns>
        public NPOI.SS.UserModel.IFont FindFont(short boldWeight, short color, short fontHeight,
                         String name, bool italic, bool strikeout,
                         FontSuperScript typeOffset, FontUnderlineType underline)
        {
            //        Console.WriteLine( boldWeight + ", " + color + ", " + fontHeight + ", " + name + ", " + italic + ", " + strikeout + ", " + typeOffset + ", " + Underline );
            for (short i = 0; i <= this.NumberOfFonts; i++)
            {
                // Remember - there is no 4!
                if (i == 4)
                    continue;

                NPOI.SS.UserModel.IFont hssfFont = GetFontAt(i);
                //            Console.WriteLine( hssfFont.GetBoldweight() + ", " + hssfFont.GetColor() + ", " + hssfFont.FontHeight + ", " + hssfFont.FontName + ", " + hssfFont.GetItalic() + ", " + hssfFont.GetStrikeout() + ", " + hssfFont.GetTypeOffset() + ", " + hssfFont.Underline );
                if (hssfFont.Boldweight == boldWeight
                        && hssfFont.Color == color
                        && hssfFont.FontHeight == fontHeight
                        && hssfFont.FontName.Equals(name)
                        && hssfFont.IsItalic == italic
                        && hssfFont.IsStrikeout == strikeout
                        && hssfFont.TypeOffset == typeOffset
                        && hssfFont.Underline == underline)
                {
                    //                Console.WriteLine( "Found font" );
                    return hssfFont;
                }
            }

            //        Console.WriteLine( "No font found" );
            return null;
        }
        /// <summary>
        /// 行内单元格常用样式设置
        /// </summary>
        /// <param name="workbook">“Excel”文件对象</param>
        /// <param name="hAlignment">水平布局方式</param>
        /// <param name="vAlignment">垂直布局方式</param>
        /// <param name="fontHeightInPoints">字体大小</param>
        /// <param name="isAddBorder">是否需要边框</param>
        /// <param name="boldWeight">字体加粗 (None = 0,Normal = 400,Bold = 700</param>
        /// <param name="fontName">字体(仿宋,楷体,宋体,微软雅黑...与Excel主题字体相对应)</param>
        /// <param name="isAddBorderColor">是否增加边框颜色</param>
        /// <param name="isItalic">是否将文字变为斜体</param>
        /// <param name="isLineFeed">是否自动换行</param>
        /// <param name="isAddCellBackground">是否增加单元格背景颜色</param>
        /// <param name="fillPattern">填充图案样式(FineDots 细点,SolidForeground立体前景,isAddFillPattern=true时存在)</param>
        /// <param name="cellBackgroundColor">单元格背景颜色(当isAddCellBackground=true时存在)</param>
        /// <param name="fontColor">字体颜色</param>
        /// <param name="underlineStyle">下划线样式(无下划线[None],单下划线[Single],双下划线[Double],会计用单下划线[SingleAccounting],会计用双下划线[DoubleAccounting])</param>
        /// <param name="typeOffset">字体上标下标(普通默认值[None],上标[Sub],下标[Super]),即字体在单元格内的上下偏移量</param>
        /// <param name="isStrikeout">是否显示删除线</param>
        /// <returns></returns>
        public static HSSFCellStyle CreateStyle(HSSFWorkbook workbook, HorizontalAlignment hAlignment, VerticalAlignment vAlignment
                                                , short fontHeightInPoints, bool isAddBorder, short boldWeight, string fontName = "宋体", bool isAddBorderColor = true
                                                , bool isItalic                    = false, bool isLineFeed = false, bool isAddCellBackground = false, FillPattern fillPattern = FillPattern.NoFill
                                                , short cellBackgroundColor        = HSSFColor.Yellow.Index, short fontColor = HSSFColor.Black.Index
                                                , FontUnderlineType underlineStyle = FontUnderlineType.None, FontSuperScript typeOffset = FontSuperScript.None, bool isStrikeout = false)
        {
            HSSFCellStyle cellStyle = (HSSFCellStyle)workbook.CreateCellStyle(); //创建列头单元格实例样式

            cellStyle.Alignment         = hAlignment;                            //水平居中
            cellStyle.VerticalAlignment = vAlignment;                            //垂直居中
            cellStyle.WrapText          = isLineFeed;                            //自动换行



            //背景颜色,边框颜色,字体颜色都是使用 HSSFColor属性中的对应调色板索引,关于 HSSFColor 颜色索引对照表,详情参考:https://www.cnblogs.com/Brainpan/p/5804167.html

            //TODO:引用了NPOI后可通过ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillPattern 为单元格背景色的填充样式

            //TODO:十分注意,要设置单元格背景色必须是FillForegroundColor和FillPattern两个属性同时设置,否则是不会显示背景颜色
            if (isAddCellBackground)
            {
                cellStyle.FillForegroundColor = cellBackgroundColor; //单元格背景颜色
                cellStyle.FillPattern         = fillPattern;         //填充图案样式(FineDots 细点,SolidForeground立体前景)
            }


            //是否增加边框
            if (isAddBorder)
            {
                //常用的边框样式 None(没有),Thin(细边框,瘦的),Medium(中等),Dashed(虚线),Dotted(星罗棋布的),Thick(厚的),Double(双倍),Hair(头发)[上右下左顺序设置]
                cellStyle.BorderBottom = BorderStyle.Thin;
                cellStyle.BorderRight  = BorderStyle.Thin;
                cellStyle.BorderTop    = BorderStyle.Thin;
                cellStyle.BorderLeft   = BorderStyle.Thin;
            }

            //是否设置边框颜色
            if (isAddBorderColor)
            {
                //边框颜色[上右下左顺序设置]
                cellStyle.TopBorderColor    = HSSFColor.DarkGreen.Index;//DarkGreen(黑绿色)
                cellStyle.RightBorderColor  = HSSFColor.DarkGreen.Index;
                cellStyle.BottomBorderColor = HSSFColor.DarkGreen.Index;
                cellStyle.LeftBorderColor   = HSSFColor.DarkGreen.Index;
            }

            /**
             * 设置相关字体样式
             */
            var cellStyleFont = (HSSFFont)workbook.CreateFont(); //创建字体

            //假如字体大小只需要是粗体的话直接使用下面该属性即可
            //cellStyleFont.IsBold = true;

            cellStyleFont.Boldweight         = boldWeight;         //字体加粗
            cellStyleFont.FontHeightInPoints = fontHeightInPoints; //字体大小
            cellStyleFont.FontName           = fontName;           //字体(仿宋,楷体,宋体 )
            cellStyleFont.Color       = fontColor;                 //设置字体颜色
            cellStyleFont.IsItalic    = isItalic;                  //是否将文字变为斜体
            cellStyleFont.Underline   = underlineStyle;            //字体下划线
            cellStyleFont.TypeOffset  = typeOffset;                //字体上标下标
            cellStyleFont.IsStrikeout = isStrikeout;               //是否有删除线

            cellStyle.SetFont(cellStyleFont);                      //将字体绑定到样式
            return(cellStyle);
        }
Beispiel #12
0
 public CStyleApplier TypeOffset(FontSuperScript value)
 {
     Font.TypeOffset = value; return(this);
 }
Beispiel #13
0
 /// <summary>
 /// Sets the superscript type.
 /// </summary>
 /// <param name="superScript">The super script.</param>
 /// <returns>The cell.</returns>
 public FluentCell SuperScript(FontSuperScript superScript)
 {
     Style.SuperScript = superScript;
     return(this);
 }
 /// <summary>
 /// Sets the superscript type.
 /// </summary>
 /// <param name="superScript">The super script.</param>
 /// <returns>The cell.</returns>
 public FluentCell SuperScript(FontSuperScript superScript)
 {
     Style.SuperScript = superScript;
     return this;
 }