/// <summary> Sees if the extended formatting record at the specified position /// represents a date. First checks against the built in formats, and /// then checks against the hash map of FormatRecords /// /// </summary> /// <param name="pos">the xf format index /// </param> /// <returns> TRUE if this format index is formatted as a Date /// </returns> public bool isDate(int pos) { XFRecord xfr = (XFRecord)xfRecords[pos]; if (xfr.isDate()) { return(true); } FormatRecord fr = (FormatRecord)formats[xfr.FormatRecord]; return(fr == null?false:fr.Date); }
/// <summary> Gets the NumberFormatInfo used to format the cell. /// /// </summary> /// <param name="pos">the xf format index /// </param> /// <returns> the DateFormat object used to format the date in the original /// excel cell /// </returns> public NumberFormatInfo getNumberFormat(int pos) { XFRecord xfr = (XFRecord)xfRecords[pos]; if (xfr.isNumber()) { return(xfr.NumberFormat); } FormatRecord fr = (FormatRecord)formats[xfr.FormatRecord]; if (fr == null) { return(null); } return(fr.Number?fr.NumberFormat:null); }
/// <summary> Gets the DateFormat used to format the cell. /// /// </summary> /// <param name="pos">the xf format index /// </param> /// <returns> the DateFormat object used to format the date in the original /// excel cell /// </returns> public DateTimeFormatInfo getDateFormat(int pos) { XFRecord xfr = (XFRecord)xfRecords[pos]; if (xfr.isDate()) { return(xfr.DateFormat); } FormatRecord fr = (FormatRecord)formats[xfr.FormatRecord]; if (fr == null) { return(null); } return(fr.Date?fr.DateFormat:null); }
/// <summary> Adds an extended formatting record to the list. If the XF record passed /// in has not been initialized, its index is determined based on the /// xfRecords list, and /// this position is passed to the XF records initialize method /// /// </summary> /// <param name="xf">the xf record to add /// </param> /// <exception cref=""> NumFormatRecordsException /// </exception> public void addStyle(XFRecord xf) { if (!xf.isInitialized()) { int pos = xfRecords.Count; xf.initialize(pos, this, fonts); xfRecords.Add(xf); } else { // The XF record has probably been read in. If the index is greater // Than the size of the list, then it is not a preset format, // so add it if (xf.getXFIndex() >= xfRecords.Count) { xfRecords.Add(xf); } } }
/// <summary> Copy constructor. Used for copying writable formats, typically /// when duplicating formats to handle merged cells /// /// </summary> /// <param name="fmt">XFRecord /// </param> protected internal XFRecord(XFRecord fmt) : base(NExcel.Biff.Type.XF) { initialized = false; locked = fmt.locked; hidden = fmt.hidden; align = fmt.align; valign = fmt.valign; orientation = fmt.orientation; wrap = fmt.wrap; leftBorder = fmt.leftBorder; rightBorder = fmt.rightBorder; topBorder = fmt.topBorder; bottomBorder = fmt.bottomBorder; leftBorderColour = fmt.leftBorderColour; rightBorderColour = fmt.rightBorderColour; topBorderColour = fmt.topBorderColour; bottomBorderColour = fmt.bottomBorderColour; pattern = fmt.pattern; xfFormatType = fmt.xfFormatType; shrinkToFit = fmt.shrinkToFit; parentFormat = fmt.parentFormat; backgroundColour = fmt.backgroundColour; // Shallow copy is sufficient for these purposes font = fmt.font; format = fmt.format; fontIndex = fmt.fontIndex; formatIndex = fmt.formatIndex; formatInfoInitialized = fmt.formatInfoInitialized; biffType = biff8; read = false; copied = true; }
/// <summary> Rationalizes the cell formats. Duplicate /// formats are removed and the format indexed of the cells /// adjusted accordingly /// /// </summary> /// <param name="fontMapping">the font mapping index numbers /// </param> /// <param name="formatMapping">the format mapping index numbers /// </param> /// <returns> the list of new font index number /// </returns> public virtual IndexMapping rationalize(IndexMapping fontMapping, IndexMapping formatMapping) { // Update the index codes for the XF records using the format // mapping and the font mapping // at the same time foreach (XFRecord xfr in xfRecords) { if (xfr.FormatRecord >= customFormatStartIndex) { xfr.FormatIndex = formatMapping.getNewIndex(xfr.FormatRecord); } xfr.FontIndex = fontMapping.getNewIndex(xfr.FontIndex); } ArrayList newrecords = new ArrayList(minXFRecords); IndexMapping mapping = new IndexMapping(xfRecords.Count); int numremoved = 0; // Copy across the fundamental styles for (int i = 0; i < minXFRecords; i++) { newrecords.Add(xfRecords[i]); mapping.setMapping(i, i); } // Iterate through the old list for (int i = minXFRecords; i < xfRecords.Count; i++) { XFRecord xf = (XFRecord)xfRecords[i]; // Compare against formats already on the list bool duplicate = false; foreach (XFRecord xf2 in newrecords) { if (duplicate) { break; } if (xf2.Equals(xf)) { duplicate = true; mapping.setMapping(i, mapping.getNewIndex(xf2.getXFIndex())); numremoved++; } } // If this format is not a duplicate then add it to the new list if (!duplicate) { newrecords.Add(xf); mapping.setMapping(i, i - numremoved); } } // It is sufficient to merely change the xf index field on all XFRecords // In this case, CellValues which refer to defunct format records // will nevertheless be written out with the correct index number foreach (XFRecord xf in xfRecords) { xf.rationalize(mapping); } // Set the new list xfRecords = newrecords; return(mapping); }
/// <summary> A public copy constructor which can be used for copy formats between /// different sheets. Unlike the the other copy constructor, this /// version does a deep copy /// /// </summary> /// <param name="cellFormat">the format to copy /// </param> protected internal XFRecord(CellFormat cellFormat) : base(NExcel.Biff.Type.XF) { Assert.verify(cellFormat is XFRecord); XFRecord fmt = (XFRecord)cellFormat; if (!fmt.formatInfoInitialized) { fmt.initializeFormatInformation(); } locked = fmt.locked; hidden = fmt.hidden; align = fmt.align; valign = fmt.valign; orientation = fmt.orientation; wrap = fmt.wrap; leftBorder = fmt.leftBorder; rightBorder = fmt.rightBorder; topBorder = fmt.topBorder; bottomBorder = fmt.bottomBorder; leftBorderColour = fmt.leftBorderColour; rightBorderColour = fmt.rightBorderColour; topBorderColour = fmt.topBorderColour; bottomBorderColour = fmt.bottomBorderColour; pattern = fmt.pattern; xfFormatType = fmt.xfFormatType; parentFormat = fmt.parentFormat; shrinkToFit = fmt.shrinkToFit; backgroundColour = fmt.backgroundColour; // Deep copy of the font font = new FontRecord(fmt.Font); // Copy the format if (fmt.Format == null) { // format is writable if (fmt.format.isBuiltIn()) { format = fmt.format; } else { // Format is not built in, so do a deep copy format = new FormatRecord((FormatRecord)fmt.format); } } else if (fmt.Format is BuiltInFormat) { // read excel format is built in excelFormat = (BuiltInFormat)fmt.excelFormat; format = (BuiltInFormat)fmt.excelFormat; } else { // read excel format is user defined Assert.verify(fmt.formatInfoInitialized); // in this case FormattingRecords should initialize the excelFormat // field with an instance of FormatRecord Assert.verify(fmt.excelFormat is FormatRecord); // Format is not built in, so do a deep copy FormatRecord fr = new FormatRecord((FormatRecord)fmt.excelFormat); // Set both format fields to be the same object, since // FormatRecord implements all the necessary interfaces excelFormat = fr; format = fr; } biffType = biff8; // The format info should be all OK by virtue of the deep copy formatInfoInitialized = true; // This format was not read in read = false; // Treat this as a new cell record, so set the copied flag to false copied = false; // The font or format indexes need to be set, so set initialized to false initialized = false; }
/// <summary> Equals method. This is called when comparing writable formats /// in order to prevent duplicate formats being added to the workbook /// /// </summary> /// <param name="o">object to compare /// </param> /// <returns> TRUE if the objects are equal, FALSE otherwise /// </returns> public override bool Equals(System.Object o) { if (o == this) { return(true); } if (!(o is XFRecord)) { return(false); } XFRecord xfr = (XFRecord)o; // Both records must be writable and have their format info initialized if (!formatInfoInitialized) { initializeFormatInformation(); } if (!xfr.formatInfoInitialized) { xfr.initializeFormatInformation(); } if (xfFormatType != xfr.xfFormatType || parentFormat != xfr.parentFormat || locked != xfr.locked || hidden != xfr.hidden || usedAttributes != xfr.usedAttributes) { return(false); } if (align != xfr.align || valign != xfr.valign || orientation != xfr.orientation || wrap != xfr.wrap || shrinkToFit != xfr.shrinkToFit) { return(false); } if (leftBorder != xfr.leftBorder || rightBorder != xfr.rightBorder || topBorder != xfr.topBorder || bottomBorder != xfr.bottomBorder) { return(false); } if (leftBorderColour != xfr.leftBorderColour || rightBorderColour != xfr.rightBorderColour || topBorderColour != xfr.topBorderColour || bottomBorderColour != xfr.bottomBorderColour) { return(false); } if (backgroundColour != xfr.backgroundColour || pattern != xfr.pattern) { return(false); } // Sufficient to just do shallow equals on font, format objects, // since we are testing for the presence of clones anwyay // Use indices rather than objects because of the rationalization // process (which does not set the object on an XFRecord) if (fontIndex != xfr.fontIndex || formatIndex != xfr.formatIndex) { return(false); } return(true); }