/// <summary> /// Updates cell formatting based on supplied co-ordinate and the co-ordinate position within the container. /// Formatting may be influenced by the current position within container. /// </summary> /// <param name="container">The container this cell is within</param> /// <param name="fromColumnIndex">The excel 'from' column</param> /// <param name="fromRowIndex">The excel 'from' row</param> /// <param name="toColumnIndex">The excel 'to' column</param> /// <param name="toRowIndex">The excel 'to' row</param> public void Update(ExcelMapCoOrdinateContainer container, int fromColumnIndex, int fromRowIndex, int toColumnIndex, int toRowIndex) { if (container == null) { throw new ArgumentNullException("container"); } if (container.Styles != null) { // Update border thickness (if there is one) ExcelCellBorderInfo conditionalBorderInfo = CreateConditionalBorderInfo(container, fromColumnIndex, fromRowIndex, toColumnIndex, toRowIndex); this.UpdateBorderInfo(conditionalBorderInfo); foreach (var style in container.Styles) { // Update fill colour (if not already set) this.UpdateFillColour(style); // Update font (if not already set) this.UpdateFontInfo(style); // Update alignment (if not already set) this.UpdateAlignmentInfo(style); // Update the excel number format (if not already set) this.UpdateNumberFormat(style); } } }
/// <summary> /// Returns true if this = other /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(ExcelCellStyleInfo other) { if (other == null && !this.HasCellInfo) { return(true); } if (other == null) { return(false); } if (!Nullable <Color> .Equals(this.FillColour, other.FillColour)) { return(false); } if (!string.Equals(this.NumberFormat, other.NumberFormat)) { return(false); } if (!ExcelCellFontInfo.Equals(this.FontInfo, other.FontInfo)) { return(false); } if (!ExcelCellBorderInfo.Equals(this.BorderInfo, other.BorderInfo)) { return(false); } if (!ExcelCellAlignmentInfo.Equals(this.AlignmentInfo, other.AlignmentInfo)) { return(false); } return(true); }
/// <summary> /// Applies border on top of this border.<br/> /// Where the frame thickness is 0, the underlying border is not updated,<br/> /// otherwise it is over-written with a new border, using the applied colour. /// </summary> /// <param name="rowHeight"></param> public static void UpdateBorder(this ExcelCellBorderInfo borderToUpdate, StyleBase styleToApply) { // Anything to apply? if (styleToApply == null || styleToApply.HasAnyBorder() == false) { return; } // Apply updates to the thickness (ie. any non-zero values) if (styleToApply.HasLeftBorder()) { borderToUpdate.WidthLeft = styleToApply.BorderThickness.Value.Left; borderToUpdate.ColourLeft = styleToApply.BorderColour; } if (styleToApply.HasTopBorder()) { borderToUpdate.WidthTop = styleToApply.BorderThickness.Value.Top; borderToUpdate.ColourTop = styleToApply.BorderColour; } if (styleToApply.HasRightBorder()) { borderToUpdate.WidthRight = styleToApply.BorderThickness.Value.Right; borderToUpdate.ColourRight = styleToApply.BorderColour; } if (styleToApply.HasBottomBorder()) { borderToUpdate.WidthBottom = styleToApply.BorderThickness.Value.Bottom; borderToUpdate.ColourBottom = styleToApply.BorderColour; } }
/// <summary> /// Extension methods that can be used with <see cref="ExcelCellBorderInfo"/>. /// </summary> /// <param name="rowHeight"></param> public static void UpdateBorder(this ExcelCellBorderInfo borderToUpdate, ExcelCellBorderInfo borderInfoToApply) { // Anything to apply? if (borderInfoToApply == null || borderInfoToApply.HasBorder == false) { return; } // Apply updates to the thickness (ie. any non-zero values) if (borderInfoToApply.HasLeftBorder) { borderToUpdate.WidthLeft = borderInfoToApply.WidthLeft; borderToUpdate.ColourLeft = borderInfoToApply.ColourLeft; } if (borderInfoToApply.HasTopBorder) { borderToUpdate.WidthTop = borderInfoToApply.WidthTop; borderToUpdate.ColourTop = borderInfoToApply.ColourTop; } if (borderInfoToApply.HasRightBorder) { borderToUpdate.WidthRight = borderInfoToApply.WidthRight; borderToUpdate.ColourRight = borderInfoToApply.ColourRight; } if (borderInfoToApply.HasBottomBorder) { borderToUpdate.WidthBottom = borderInfoToApply.WidthBottom; borderToUpdate.ColourBottom = borderInfoToApply.ColourBottom; } }
/// <summary> /// Creates a copy of original. /// </summary> /// <param name="source"></param> private ExcelCellStyleInfo(ExcelCellStyleInfo source) { this.fillColour = source.FillColour; this.numberFormat = source.NumberFormat; this.hasCellInfo = source.HasCellInfo; this.fontInfo = source.FontInfo == null ? null : (ExcelCellFontInfo)source.FontInfo.Clone(); this.borderInfo = source.BorderInfo == null ? null : (ExcelCellBorderInfo)source.BorderInfo.Clone(); this.alignmentInfo = source.AlignmentInfo == null ? null : (ExcelCellAlignmentInfo)source.AlignmentInfo.Clone(); }
/// <summary> /// Creates a <see cref="ExcelCellBorderInfo"/> from the border colour and thickness specified in a <see cref="StyleBase"/> derived class instance /// which has uniform border thickness and border colour. /// NB! This is intended to raise an error if either border colour or thickness is null/not specified. /// </summary> /// <param name="mapStyle"></param> /// <returns></returns> private static ExcelCellBorderInfo CreateBorderInfo(StyleBase mapStyle) { var borderInfo = new ExcelCellBorderInfo(); borderInfo.ColourLeft = mapStyle.BorderColour.Value; borderInfo.ColourTop = mapStyle.BorderColour.Value; borderInfo.ColourRight = mapStyle.BorderColour.Value; borderInfo.ColourBottom = mapStyle.BorderColour.Value; borderInfo.WidthLeft = mapStyle.BorderThickness.Value.Left; borderInfo.WidthTop = mapStyle.BorderThickness.Value.Top; borderInfo.WidthRight = mapStyle.BorderThickness.Value.Right; borderInfo.WidthBottom = mapStyle.BorderThickness.Value.Bottom; return(borderInfo); }
/// <summary> /// Creates and returns a conditional border thickness which is dependent on where we are within the container. /// </summary> /// <param name="container">The container this cell is within</param> /// <param name="fromColumnIndex">The excel 'from' column</param> /// <param name="fromRowIndex">The excel 'from' row</param> /// <param name="toColumnIndex">The excel 'to' column</param> /// <param name="toRowIndex">The excel 'to' row</param> /// <returns></returns> private static ExcelCellBorderInfo CreateConditionalBorderInfo(ExcelMapCoOrdinateContainer container, int fromColumnIndex, int fromRowIndex, int toColumnIndex, int toRowIndex) { var newBorderInfo = new ExcelCellBorderInfo(); if (container.Styles != null) { foreach (var style in container.Styles) { // Update border thickness (if there is one either conditionally on location or unconditionally) if (style.HasAnyBorder()) { // Only apply if currently left-most if (container.ExcelColumnStart == fromColumnIndex && style.BorderThickness.Value.Left > 0) { newBorderInfo.WidthLeft = style.BorderThickness.Value.Left; newBorderInfo.ColourLeft = style.BorderColour.Value; } // Only apply if currently top-most if (container.ExcelRowStart == fromRowIndex && style.BorderThickness.Value.Top > 0) { newBorderInfo.WidthTop = style.BorderThickness.Value.Top; newBorderInfo.ColourTop = style.BorderColour.Value; } // Only apply if currently right-most uint endColumnIndex = container.GetEndColumnIndex(); if (endColumnIndex == toColumnIndex && style.BorderThickness.Value.Right > 0) { newBorderInfo.WidthRight = style.BorderThickness.Value.Right; newBorderInfo.ColourRight = style.BorderColour.Value; } // Only apply if currently bottom-most uint endRowIndex = container.GetEndRowIndex(); if (endRowIndex == toRowIndex && style.BorderThickness.Value.Bottom > 0) { newBorderInfo.WidthBottom = style.BorderThickness.Value.Bottom; newBorderInfo.ColourBottom = style.BorderColour.Value; } } } } return(newBorderInfo); }
/// <summary> /// Compares and updates border thickness for the cell /// </summary> /// <param name="rowHeight"></param> private void UpdateBorderInfo(StyleBase mapStyle) { // Anything to apply? if (mapStyle != null && mapStyle.HasAnyBorder()) { if (this.borderInfo == null) { // Clone the border and apply to the cell this.borderInfo = CreateBorderInfo(mapStyle); this.hasCellInfo = true; } else { // Update the border with layered information this.borderInfo.UpdateBorder(mapStyle); } } }
/// <summary> /// Compares and updates border thickness for the cell.<br/> /// </summary> /// <param name="rowHeight"></param> private void UpdateBorderInfo(ExcelCellBorderInfo borderInfo) { // Anything to apply? if (borderInfo.HasBorder) { // Any current border? if (this.borderInfo == null) { // No current border set, but we have a border to set - Apply this.borderInfo = borderInfo; this.hasCellInfo = true; } else { // Update the border with layered information this.borderInfo.UpdateBorder(borderInfo); } } }
/// <summary> /// Creates and appends a new border to the supplied <see cref="Stylesheet"/> based on properties of the <see cref="ExcelCellBorderInfo"/>.<br/> /// Returns the index of the newly created border. /// </summary> /// <param name="stylesheet"></param> /// <param name="borderInfo"></param> /// <returns></returns> private static uint CreateNewBorder(Stylesheet stylesheet, ExcelCellBorderInfo borderInfo) { Border border = new Border(); // LEFT if (borderInfo.HasLeftBorder) { border.LeftBorder = new LeftBorder() { Color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue(StyleTranslator.Translate(borderInfo.ColourLeft.Value)) }, Style = StyleTranslator.Translate(borderInfo.WidthLeft) }; } // TOP if (borderInfo.HasTopBorder) { border.TopBorder = new TopBorder() { Color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue(StyleTranslator.Translate(borderInfo.ColourTop.Value)) }, Style = StyleTranslator.Translate(borderInfo.WidthTop) }; } // RIGHT if (borderInfo.HasRightBorder) { border.RightBorder = new RightBorder() { Color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue(StyleTranslator.Translate(borderInfo.ColourRight.Value)) }, Style = StyleTranslator.Translate(borderInfo.WidthRight) }; } // BOTTOM if (borderInfo.HasBottomBorder) { border.BottomBorder = new BottomBorder() { Color = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue(StyleTranslator.Translate(borderInfo.ColourBottom.Value)) }, Style = StyleTranslator.Translate(borderInfo.WidthBottom) }; } stylesheet.Borders.Append(border); uint borderId = (uint)new List <object>(stylesheet.Borders.Cast <object>()).IndexOf(border); return(borderId); }