当前位置: 首页>>代码示例>>C#>>正文


C# HSSFWorkbook.GetFontAt方法代码示例

本文整理汇总了C#中NPOI.HSSF.UserModel.HSSFWorkbook.GetFontAt方法的典型用法代码示例。如果您正苦于以下问题:C# HSSFWorkbook.GetFontAt方法的具体用法?C# HSSFWorkbook.GetFontAt怎么用?C# HSSFWorkbook.GetFontAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NPOI.HSSF.UserModel.HSSFWorkbook的用法示例。


在下文中一共展示了HSSFWorkbook.GetFontAt方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Test45338

        public void Test45338()
        {
            HSSFWorkbook wb = new HSSFWorkbook();
            Assert.AreEqual(4, wb.NumberOfFonts);

            ISheet s = wb.CreateSheet();
            s.CreateRow(0);
            s.CreateRow(1);
            ICell c1 = s.GetRow(0).CreateCell(0);
            ICell c2 = s.GetRow(1).CreateCell(0);

            Assert.AreEqual(4, wb.NumberOfFonts);

            IFont f1 = wb.GetFontAt((short)0);
            Assert.AreEqual(400, f1.Boldweight);

            // Check that asking for the same font
            //  multiple times gives you the same thing.
            // Otherwise, our Tests wouldn't work!
            Assert.AreEqual(
                    wb.GetFontAt((short)0),
                    wb.GetFontAt((short)0)
            );
            Assert.AreEqual(
                    wb.GetFontAt((short)2),
                    wb.GetFontAt((short)2)
            );
            Assert.IsTrue(
                    wb.GetFontAt((short)0)
                    !=
                    wb.GetFontAt((short)2)
            );

            // Look for a new font we have
            //  yet to Add
            Assert.IsNull(
                wb.FindFont(
                    (short)11, (short)123, (short)22,
                    "Thingy", false, true, (short)2, (byte)2
                )
            );

            IFont nf = wb.CreateFont();
            Assert.AreEqual(5, wb.NumberOfFonts);

            Assert.AreEqual(5, nf.Index);
            Assert.AreEqual(nf, wb.GetFontAt((short)5));

            nf.Boldweight = ((short)11);
            nf.Color = ((short)123);
            nf.FontHeight = ((short)22);
            nf.FontName = ("Thingy");
            nf.IsItalic = (false);
            nf.IsStrikeout = (true);
            nf.TypeOffset = ((short)2);
            nf.Underline = ((byte)2);

            Assert.AreEqual(5, wb.NumberOfFonts);
            Assert.AreEqual(nf, wb.GetFontAt((short)5));

            // Find it now
            Assert.IsNotNull(
                wb.FindFont(
                    (short)11, (short)123, (short)22,
                    "Thingy", false, true, (short)2, (byte)2
                )
            );
            Assert.AreEqual(
                5,
                wb.FindFont(
                       (short)11, (short)123, (short)22,
                       "Thingy", false, true, (short)2, (byte)2
                   ).Index
            );
            Assert.AreEqual(nf,
                   wb.FindFont(
                       (short)11, (short)123, (short)22,
                       "Thingy", false, true, (short)2, (byte)2
                   )
            );
        }
开发者ID:xoposhiy,项目名称:npoi,代码行数:81,代码来源:TestBugs.cs

示例2: SetFormatProperties

 /// <summary>
 /// Sets the format properties of the given style based on the given map.
 /// </summary>
 /// <param name="style">The cell style</param>
 /// <param name="workbook">The parent workbook.</param>
 /// <param name="properties">The map of format properties (String -&gt; Object).</param>
 private static void SetFormatProperties(
         NPOI.SS.UserModel.ICellStyle style, HSSFWorkbook workbook, Hashtable properties)
 {
     style.Alignment = (NPOI.SS.UserModel.HorizontalAlignment)GetShort(properties, ALIGNMENT);
     style.BorderBottom = (NPOI.SS.UserModel.BorderStyle)GetShort(properties, BORDER_BOTTOM);
     style.BorderLeft = (NPOI.SS.UserModel.BorderStyle)GetShort(properties, BORDER_LEFT);
     style.BorderRight = (NPOI.SS.UserModel.BorderStyle)GetShort(properties, BORDER_RIGHT);
     style.BorderTop = (NPOI.SS.UserModel.BorderStyle)GetShort(properties, BORDER_TOP);
     style.BottomBorderColor = (GetShort(properties, BOTTOM_BORDER_COLOR));
     style.DataFormat = (GetShort(properties, DATA_FORMAT));
     style.FillBackgroundColor = (GetShort(properties, FILL_BACKGROUND_COLOR));
     style.FillForegroundColor = (GetShort(properties, FILL_FOREGROUND_COLOR));
     style.FillPattern = (NPOI.SS.UserModel.FillPatternType)GetShort(properties, FILL_PATTERN);
     style.SetFont(workbook.GetFontAt(GetShort(properties, FONT)));
     style.IsHidden = (GetBoolean(properties, HIDDEN));
     style.Indention = (GetShort(properties, INDENTION));
     style.LeftBorderColor = (GetShort(properties, LEFT_BORDER_COLOR));
     style.IsLocked = (GetBoolean(properties, LOCKED));
     style.RightBorderColor = (GetShort(properties, RIGHT_BORDER_COLOR));
     style.Rotation = (GetShort(properties, ROTATION));
     style.TopBorderColor = (GetShort(properties, TOP_BORDER_COLOR));
     style.VerticalAlignment = (NPOI.SS.UserModel.VerticalAlignment)GetShort(properties, VERTICAL_ALIGNMENT);
     style.WrapText = (GetBoolean(properties, WRAP_TEXT));
 }
开发者ID:WPG,项目名称:npoi,代码行数:30,代码来源:HSSFCellUtil.cs

示例3: AutoSizeColumn

        /// <summary>
        /// Adjusts the column width to fit the contents.
        /// This Process can be relatively slow on large sheets, so this should
        /// normally only be called once per column, at the end of your
        /// Processing.
        /// You can specify whether the content of merged cells should be considered or ignored.
        /// Default is to ignore merged cells.
        /// </summary>
        /// <param name="column">the column index</param>
        /// <param name="useMergedCells">whether to use the contents of merged cells when calculating the width of the column</param>
        public void AutoSizeColumn(int column, bool useMergedCells)
        {
            /**
             * Excel measures columns in Units of 1/256th of a Char width
             * but the docs say nothing about what particular Char is used.
             * '0' looks to be a good choice.
             */
            char defaultChar = '0';

            /**
             * This is the multiple that the font height is scaled by when determining the
             * boundary of rotated text.
             */
            double fontHeightMultiple = 2.0;

            //FontRenderContext frc = new FontRenderContext(null, true, true);

            HSSFWorkbook wb = new HSSFWorkbook(book);
            NPOI.SS.UserModel.Font defaultFont = wb.GetFontAt((short)0);

            //str = new AttributedString("" + defaultChar);
            //CopyAttributes(defaultFont, str, 0, 1);
            //layout = new TextLayout(str.GetEnumerator(), frc);
            System.Drawing.Font font = HSSFFont2Font((HSSFFont)defaultFont);
            int defaultCharWidth = TextRenderer.MeasureText("" + new String(defaultChar, 1), font).Width;

            double width = -1;

            bool skipthisrow = false;
            for (IEnumerator it = rows.Values.GetEnumerator(); it.MoveNext(); )
            {
                HSSFRow row = (HSSFRow)it.Current;
                NPOI.SS.UserModel.Cell cell = (HSSFCell)row.GetCell(column);

                if (cell == null) continue;

                int colspan = 1;
                for (int i = 0; i < NumMergedRegions; i++)
                {
                    NPOI.SS.Util.CellRangeAddress region = GetMergedRegion(i);
                    if (ContainsCell(region, row.RowNum, column))
                    {
                        if (!useMergedCells)
                        {
                            // If we're not using merged cells, skip this one and move on to the next.
                            skipthisrow = true;
                        }
                        cell = row.GetCell(region.FirstColumn);
                        colspan = 1 + region.LastColumn - region.FirstColumn;
                    }
                }
                if (skipthisrow)
                {
                    continue;
                }

                NPOI.SS.UserModel.CellStyle style = cell.CellStyle;
                NPOI.SS.UserModel.Font font1 = wb.GetFontAt(style.FontIndex);

                if (cell.CellType == NPOI.SS.UserModel.CellType.STRING)
                {
                    HSSFRichTextString rt = (HSSFRichTextString)cell.RichStringCellValue;
                    String[] lines = rt.String.Split(new char[] { '\n' });
                    for (int k = 0; k < lines.Length; k++)
                    {
                        String txt = lines[k] + defaultChar;
                        //str = new AttributedString(txt);
                        //copyAttributes(font1, str, 0, txt.Length);

                        if (rt.NumFormattingRuns > 0)
                        {
                            for (int j = 0; j < lines[k].Length; j++)
                            {
                                int idx = rt.GetFontAtIndex(j);
                                if (idx != 0)
                                {
                                    NPOI.SS.UserModel.Font fnt = wb.GetFontAt((short)idx);
                                    //copyAttributes(fnt, str, j, j + 1);
                                }
                            }
                        }

                        //layout = new TextLayout(str.GetEnumerator(), frc);
                        if (style.Rotation != 0)
                        {
                            /*
                             * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                             * and then rotate the text before computing the bounds. The scale results in some whitespace around
                             * the Unrotated top and bottom of the text that normally wouldn't be present if Unscaled, but
                             * is Added by the standard Excel autosize.
//.........这里部分代码省略.........
开发者ID:babywzazy,项目名称:Server,代码行数:101,代码来源:HSSFSheet.cs


注:本文中的NPOI.HSSF.UserModel.HSSFWorkbook.GetFontAt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。