private static ICellStyle CopyStyle(this ICellStyle dCellStyle, ICellStyle sCellStyle, IWorkbook dWb, IWorkbook sWb, List <ICellStyle> dCellStyles, List <IFont> dFonts) { ICellStyle currCellStyle = dCellStyle; currCellStyle.Alignment = sCellStyle.Alignment; currCellStyle.VerticalAlignment = sCellStyle.VerticalAlignment; currCellStyle.BorderTop = sCellStyle.BorderTop; currCellStyle.BorderBottom = sCellStyle.BorderBottom; currCellStyle.BorderLeft = sCellStyle.BorderLeft; currCellStyle.BorderRight = sCellStyle.BorderRight; currCellStyle.TopBorderColor = sCellStyle.TopBorderColor; currCellStyle.LeftBorderColor = sCellStyle.LeftBorderColor; currCellStyle.RightBorderColor = sCellStyle.RightBorderColor; currCellStyle.BottomBorderColor = sCellStyle.BottomBorderColor; currCellStyle.Indention = sCellStyle.Indention; currCellStyle.IsHidden = sCellStyle.IsHidden; currCellStyle.IsLocked = sCellStyle.IsLocked; currCellStyle.Rotation = sCellStyle.Rotation; currCellStyle.ShrinkToFit = sCellStyle.ShrinkToFit; currCellStyle.WrapText = sCellStyle.WrapText; currCellStyle.DataFormat = dWb.CreateDataFormat().GetFormat(sWb.CreateDataFormat().GetFormat(sCellStyle.DataFormat)); IFont sFont = sCellStyle.GetFont(sWb); IFont dFont = dWb.FindFont(sFont, dFonts) ?? dWb.CreateFont().CopyFont(sFont, dFonts); currCellStyle.SetFont(dFont); dCellStyles.Add(currCellStyle); return(currCellStyle); }
public static IFont GetFont(this IWorkbook wb, SortedDictionary <string, string> fontdic) { var weight = fontdic.FontWeight(); var color = fontdic.FontColor(); var size = fontdic.FontSize(); var name = fontdic.FontName(); var underline = fontdic.FontUnderline(); var italic = fontdic.FontItalic(); var strikeout = fontdic.FontStrikeout(); var offset = fontdic.ConvertToSuperScript(); var findHeight = (short)(size * 20); var font = wb.FindFont(weight, color, findHeight, name, italic, strikeout, offset, underline); if (font == null) { font = wb.CreateFont(); font.Boldweight = weight; font.Color = color; font.FontHeightInPoints = size; font.FontName = name; font.Underline = underline; font.IsItalic = italic; font.IsStrikeout = strikeout; font.TypeOffset = offset; } return(font); }
public void BaseTestDefaultFont(String defaultName, short defaultSize, short defaultColor) { //get default font and check against default value IWorkbook workbook = _testDataProvider.CreateWorkbook(); IFont fontFind = workbook.FindFont((short)FontBoldWeight.NORMAL, defaultColor, defaultSize, defaultName, false, false, FontFormatting.SS_NONE, (byte)FontUnderlineType.NONE); Assert.IsNotNull(fontFind); //get default font, then change 2 values and check against different values (height Changes) IFont font = workbook.CreateFont(); font.Boldweight = (short)(FontBoldWeight.BOLD); Assert.AreEqual((short)FontBoldWeight.BOLD, font.Boldweight); font.Underline = (byte)(FontUnderlineType.DOUBLE); Assert.AreEqual((byte)FontUnderlineType.DOUBLE, font.Underline); font.FontHeightInPoints = ((short)15); Assert.AreEqual(15 * 20, font.FontHeight); Assert.AreEqual(15, font.FontHeightInPoints); fontFind = workbook.FindFont((short)FontBoldWeight.BOLD, defaultColor, (short)(15 * 20), defaultName, false, false, FontFormatting.SS_NONE, (byte)FontUnderlineType.DOUBLE); Assert.IsNotNull(fontFind); }
public void BaseTestDefaultFont(string defaultName, short defaultSize, short defaultColor) { //get default font and check against default value IWorkbook workbook = _testDataProvider.CreateWorkbook(); IFont fontFind = workbook.FindFont((short)FontBoldWeight.Normal, defaultColor, defaultSize, defaultName, false, false, FontSuperScript.None, FontUnderlineType.None); Assert.IsNotNull(fontFind); //get default font, then change 2 values and check against different values (height Changes) IFont font = workbook.CreateFont(); font.Boldweight = (short)(FontBoldWeight.Bold); Assert.AreEqual((short)FontBoldWeight.Bold, font.Boldweight); font.Underline = FontUnderlineType.Double; Assert.AreEqual(FontUnderlineType.Double, font.Underline); font.FontHeightInPoints = ((short)15); Assert.AreEqual(15 * 20, font.FontHeight); Assert.AreEqual(15, font.FontHeightInPoints); fontFind = workbook.FindFont((short)FontBoldWeight.Bold, defaultColor, (short)(15 * 20), defaultName, false, false, FontSuperScript.None, FontUnderlineType.Double); Assert.IsNotNull(fontFind); }
// 取得相似的 CellStyle 來做使用 // [problem] 應該還是會爆吧?! private ICellStyle GetCellStyleByFontColor(IWorkbook WorkBook, ICell Cell, short sColor) { ////------------------------------- //// 找舊的 //for (short Index = 0; Index < 255; Index++) //{ // ICellStyle iCellStyle = WorkBook.GetCellStyleAt(Index); // IFont iFont = iCellStyle.GetFont(WorkBook); // if (sColor == iFont.Color) // return iCellStyle; //} ////------------------------------- //// 產生新的 CellStyle //ICellStyle CellStyle = ExcelExUtility.CreateCellStyle(WorkBook, Cell.CellStyle); //// 產生新的 Font //IFont font = ExcelExUtility.CreateFontFrom(WorkBook, Cell.CellStyle.GetFont(WorkBook)); //// 設定 Font //font.Color = sColor; //CellStyle.SetFont(font); //// 傳回結果 //return CellStyle; //------------------------------- // 產生新的 CellStyle ICellStyle CellStyle = ExcelExUtility.CreateCellStyle(WorkBook, Cell.CellStyle); // 產生新的 Font IFont font = WorkBook.FindFont(400, sColor, 240, "新細明體", false, false, 0, 0); if (font == null) { font = ExcelExUtility.CreateFontFrom(WorkBook, Cell.CellStyle.GetFont(WorkBook)); font.Color = sColor; } // 設定 Font CellStyle.SetFont(font); // 傳回結果 return(CellStyle); }
private static ICellStyle FindStyle(this IWorkbook dWb, IWorkbook sWb, ICellStyle style, List <ICellStyle> dCellStyles, List <IFont> dFonts) { ICellStyle dStyle = null; foreach (ICellStyle currStyle in dCellStyles) { if (currStyle.Alignment != style.Alignment) { continue; } else if (currStyle.VerticalAlignment != style.VerticalAlignment) { continue; } else if (currStyle.BorderTop != style.BorderTop) { continue; } else if (currStyle.BorderBottom != style.BorderBottom) { continue; } else if (currStyle.BorderLeft != style.BorderLeft) { continue; } else if (currStyle.BorderRight != style.BorderRight) { continue; } else if (currStyle.TopBorderColor != style.TopBorderColor) { continue; } else if (currStyle.BottomBorderColor != style.BottomBorderColor) { continue; } else if (currStyle.LeftBorderColor != style.LeftBorderColor) { continue; } else if (currStyle.RightBorderColor != style.RightBorderColor) { continue; } else if (currStyle.Indention != style.Indention) { continue; } else if (currStyle.IsHidden != style.IsHidden) { continue; } else if (currStyle.IsLocked != style.IsLocked) { continue; } else if (currStyle.Rotation != style.Rotation) { continue; } else if (currStyle.ShrinkToFit != style.ShrinkToFit) { continue; } else if (currStyle.WrapText != style.WrapText) { continue; } else if (!currStyle.GetDataFormatString().Equals(style.GetDataFormatString())) { continue; } else { IFont sFont = sWb.GetFontAt(style.FontIndex); IFont dFont = dWb.FindFont(sFont, dFonts); if (dFont == null) { continue; } else { currStyle.SetFont(dFont); dStyle = currStyle; break; } } } return(dStyle); }
public void Test45338() { IWorkbook wb = _testDataProvider.CreateWorkbook(); int num0 = wb.NumberOfFonts; ISheet s = wb.CreateSheet(); s.CreateRow(0); s.CreateRow(1); s.GetRow(0).CreateCell(0); s.GetRow(1).CreateCell(0); //default font IFont f1 = wb.GetFontAt((short)0); Assert.AreEqual((short)FontBoldWeight.NORMAL, f1.Boldweight); // Check that asking for the same font // multiple times gives you the same thing. // Otherwise, our Tests wouldn't work! Assert.AreSame(wb.GetFontAt((short)0), wb.GetFontAt((short)0)); // Look for a new font we have // yet to add Assert.IsNull( wb.FindFont( (short)FontBoldWeight.BOLD, (short)123, (short)(22 * 20), "Thingy", false, true, (short)2, (byte)2 ) ); IFont nf = wb.CreateFont(); short nfIdx = nf.Index; Assert.AreEqual(num0 + 1, wb.NumberOfFonts); Assert.AreEqual(nf, wb.GetFontAt(nfIdx)); nf.Boldweight = (short)(FontBoldWeight.BOLD); nf.Color = (short)123; nf.FontHeightInPoints = (short)22; nf.FontName = ("Thingy"); nf.IsItalic = (false); nf.IsStrikeout = (true); nf.TypeOffset = (short)2; nf.Underline = (byte)2; Assert.AreEqual(num0 + 1, wb.NumberOfFonts); Assert.AreEqual(nf, wb.GetFontAt(nfIdx)); Assert.AreEqual(wb.GetFontAt(nfIdx), wb.GetFontAt(nfIdx)); Assert.IsTrue(wb.GetFontAt((short)0) != wb.GetFontAt(nfIdx)); // Find it now Assert.IsNotNull( wb.FindFont( (short)FontBoldWeight.BOLD, (short)123, (short)(22 * 20), "Thingy", false, true, (short)2, (byte)2 ) ); Assert.AreSame(nf, wb.FindFont( (short)FontBoldWeight.BOLD, (short)123, (short)(22 * 20), "Thingy", false, true, (short)2, (byte)2 ) ); }