Beispiel #1
0
        public void SetFont()
        {
            IWorkbook wb  = _testDataProvider.CreateWorkbook();
            ISheet    sh  = wb.CreateSheet();
            IRow      row = sh.CreateRow(0);
            ICell     A1  = row.CreateCell(0);
            ICell     B1  = row.CreateCell(1);
            short     defaultFontIndex = 0;
            IFont     font             = wb.CreateFont();

            font.IsItalic = true;
            short customFontIndex = font.Index;

            // Assumptions
            Assert.AreNotEqual(defaultFontIndex, customFontIndex);
            Assert.AreEqual(A1.CellStyle, B1.CellStyle);
            // should be Assert.AreSame, but a new HSSFCellStyle is returned for each GetCellStyle() call.
            // HSSFCellStyle wraps an underlying style record, and the underlying
            // style record is the same between multiple GetCellStyle() calls.
            Assert.AreEqual(defaultFontIndex, A1.CellStyle.FontIndex);
            Assert.AreEqual(defaultFontIndex, B1.CellStyle.FontIndex);

            // Get/set alignment modifies the cell's style
            CellUtil.SetFont(A1, font);
            Assert.AreEqual(customFontIndex, A1.CellStyle.FontIndex);

            // Get/set alignment doesn't affect the style of cells with
            // the same style prior to modifying the style
            Assert.AreNotEqual(A1.CellStyle, B1.CellStyle);
            Assert.AreEqual(defaultFontIndex, B1.CellStyle.FontIndex);

            wb.Close();
        }
Beispiel #2
0
        public void SetFontShouldNotCreateDuplicateStyle()
        {
            IWorkbook wb1 = _testDataProvider.CreateWorkbook();
            ICell     c   = wb1.CreateSheet().CreateRow(1).CreateCell(1);
            IFont     f   = wb1.CreateFont();

            CellUtil.SetFont(c, f);
            int num1 = wb1.NumCellStyles;

            CellUtil.SetFont(c, f);
            int num2 = wb1.NumCellStyles;

            Assert.AreEqual(num1, num2);
            wb1.Close();
        }
Beispiel #3
0
        public void SetFontFromDifferentWorkbook()
        {
            IWorkbook wb1   = _testDataProvider.CreateWorkbook();
            IWorkbook wb2   = _testDataProvider.CreateWorkbook();
            IFont     font1 = wb1.CreateFont();
            IFont     font2 = wb2.CreateFont();

            // do something to make font1 and font2 different
            // so they are not same or Equal.
            font1.IsItalic = true;
            ICell A1 = wb1.CreateSheet().CreateRow(0).CreateCell(0);

            // okay
            CellUtil.SetFont(A1, font1);

            // font belongs to different workbook
            try
            {
                CellUtil.SetFont(A1, font2);
                Assert.Fail("setFont not allowed if font belongs to a different workbook");
            }
            catch (ArgumentException e)
            {
                if (e.Message.StartsWith("Font does not belong to this workbook"))
                {
                    // expected
                }
                else
                {
                    throw e;
                }
            }
            finally
            {
                wb1.Close();
                wb2.Close();
            }
        }
Beispiel #4
0
 public static void SetFont(ICell cell, HSSFWorkbook workbook, HSSFFont font)
 {
     CellUtil.SetFont(cell, font);
 }