/// <summary> /// Clones all the style information from another /// HSSFCellStyle, onto this one. This /// HSSFCellStyle will then have all the same /// properties as the source, but the two may /// be edited independently. /// Any stylings on this HSSFCellStyle will be lost! /// The source HSSFCellStyle could be from another /// HSSFWorkbook if you like. This allows you to /// copy styles from one HSSFWorkbook to another. /// </summary> /// <param name="source">The source.</param> public void CloneStyleFrom(HSSFCellStyle source) { // First we need to clone the extended format // record _format.CloneStyleFrom(source._format); // Handle matching things if we cross workbooks if (_workbook != source._workbook) { lastDateFormat = short.MinValue; lastFormats = null; getDataFormatStringCache = null; // Then we need to clone the format string, // and update the format record for this short fmt = (short)_workbook.CreateFormat( source.GetDataFormatString() ); this.DataFormat = (fmt); // Finally we need to clone the font, // and update the format record for this FontRecord fr = _workbook.CreateNewFont(); fr.CloneStyleFrom( source._workbook.GetFontRecordAt( source.FontIndex ) ); HSSFFont font = new HSSFFont( (short)_workbook.GetFontIndex(fr), fr ); this.SetFont(font); } }
public void TestCloneOnto() { ExtendedFormatRecord base1 = CreateEFR(); ExtendedFormatRecord other = new ExtendedFormatRecord(); other.CloneStyleFrom(base1); byte[] recordBytes = other.Serialize(); Assert.AreEqual(recordBytes.Length - 4, data.Length); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], recordBytes[i + 4], "At offset " + i); } }
/** * Applying a user-defined style (UDS) is special. Excel does not directly reference user-defined styles, but * instead create a 'proxy' ExtendedFormatRecord referencing the UDS as parent. * * The proceudre to apply a UDS is as follows: * * 1. search for a ExtendedFormatRecord with parentIndex == style.getIndex() * and xfType == ExtendedFormatRecord.XF_CELL. * 2. if not found then create a new ExtendedFormatRecord and copy all attributes from the user-defined style * and set the parentIndex to be style.getIndex() * 3. return the index of the ExtendedFormatRecord, this will be assigned to the parent cell record * * @param style the user style to apply * * @return the index of a ExtendedFormatRecord record that will be referenced by the cell */ private short ApplyUserCellStyle(HSSFCellStyle style) { if (style.UserStyleName == null) { throw new ArgumentException("Expected user-defined style"); } InternalWorkbook iwb = book.Workbook; short userXf = -1; int numfmt = iwb.NumExFormats; for (short i = 0; i < numfmt; i++) { ExtendedFormatRecord xf = iwb.GetExFormatAt(i); if (xf.XFType == ExtendedFormatRecord.XF_CELL && xf.ParentIndex == style.Index) { userXf = i; break; } } short styleIndex; if (userXf == -1) { ExtendedFormatRecord xfr = iwb.CreateCellXF(); xfr.CloneStyleFrom(iwb.GetExFormatAt(style.Index)); xfr.IndentionOptions = (short)0; xfr.XFType = (ExtendedFormatRecord.XF_CELL); xfr.ParentIndex = (style.Index); styleIndex = (short)numfmt; } else { styleIndex = userXf; } return(styleIndex); }