using System; using System.Collections.Generic; using System.Text; namespace Biff8Excel.Excel { //'--------------------------------------------------------------------------------------- //' Module : ExcelCellStyle //' Author : Alan Haigh alan.haigh@salvesen.com //' Purpose : Represents the Style of a Single row, column or cell in a worksheet //'--------------------------------------------------------------------------------------- public class ExcelCellStyle : IDisposable { ExcelFont m_font; ExcelFormat m_format; Biff8Excel.Records.ExtendedFormat m_xf; internal ExcelWorkbook pWorkbook; ushort m_fontIndex; ushort m_formatIndex; int m_styleIndex; bool m_celLocked; bool m_formulaHidden; EnumType m_styleType; EnumHorizontalAlignment m_horizAlign; bool m_wrapText; EnumVerticalAlignment m_verAlign; byte m_RotatAngle; bool m_stackText; EnumLineStyle m_L_LineStyle; EnumLineStyle m_R_LineStyle; EnumLineStyle m_T_LineStyle; EnumLineStyle m_B_LineStyle; EnumLineStyle m_Bdr_LineStyle; EnumLineStyle m_D_LineStyle; bool m_D_TLBR; bool m_D_TRBL; EnumColours m_L_Colour; EnumColours m_R_Colour; EnumColours m_T_Colour; EnumColours m_B_Colour; EnumColours m_D_Colour; EnumColours m_Bdr_Colour; EnumFill m_fillPattern; EnumColours m_fillForeColour; EnumColours m_fillBackColour; bool m_styleUpdated; EnumUsedAttrib m_usedAttribute; public EnumColours BorderColour { set { m_Bdr_Colour = value; m_xf.TopLineColour = (byte)value; m_xf.LeftLineColour = (byte)value; m_xf.BottomLineColour = (byte)value; m_xf.RightLineColour = (byte)value; m_styleUpdated = true; } get { return m_Bdr_Colour; } } public EnumLineStyle BorderLineStyle { set { m_Bdr_LineStyle = value; m_xf.TopLineStyle = (byte)value; m_xf.LeftLineStyle = (byte)value; m_xf.BottomLineStyle = (byte)value; m_xf.RightLineStyle = (byte)value; m_styleUpdated = true; if (m_Bdr_LineStyle != EnumLineStyle.None) if (BorderColour == 0) BorderColour = (EnumColours)0x40; } get { return m_Bdr_LineStyle; } } public EnumColours BottomLineColour { set { m_B_Colour = value; m_xf.BottomLineColour = (byte)m_B_Colour; m_styleUpdated = true; } get { return m_B_Colour; } } public EnumLineStyle BottomLineStyle { set { m_B_LineStyle = value; m_xf.BottomLineStyle = (byte)m_B_LineStyle; m_styleUpdated = true; if (m_B_LineStyle != EnumLineStyle.None) if (m_B_Colour == 0) m_B_Colour = EnumColours.SkyBlue; } get { return m_B_LineStyle; } } public bool CellIsLocked { set { m_celLocked = value; m_xf.CellIsLocked = value; m_styleUpdated = true; } get { return m_celLocked; } } public ExcelCellStyle Clone() { //ExcelCellStyle Style = new ExcelCellStyle(pWorkbook); ExcelCellStyle Style = pWorkbook.CreateStyle(); Style.Font = m_font.Clone(); Style.Format = m_format.Clone(); Style.BorderColour = m_Bdr_Colour; Style.BorderLineStyle = m_Bdr_LineStyle; Style.BottomLineColour = m_B_Colour; Style.BottomLineStyle = m_B_LineStyle; Style.CellIsLocked = m_celLocked; Style.DiagLineColour = m_D_Colour; Style.DiagLineStyle = m_D_LineStyle; Style.DiagLineTopLeftToBottomRight = m_D_TLBR; Style.DiagLineTopRightToBottomLeft = m_D_TRBL; Style.FontIndex = m_fontIndex; Style.FormatIndex = m_formatIndex; Style.FormulaHidden = m_formulaHidden; Style.HorizontalAlignment = m_horizAlign; Style.LeftLineColour = m_L_Colour; Style.LeftLineStyle = m_L_LineStyle; Style.Pattern = m_fillPattern; Style.PatternBackColour = m_fillBackColour; Style.PatternForeColour = m_fillForeColour; Style.RightLineColour = m_R_Colour; Style.RightLineStyle = m_R_LineStyle; Style.StackText = m_stackText; Style.StyleType = m_styleType; Style.TextRotationAngle = m_RotatAngle; Style.TopLineColour = m_T_Colour; Style.TopLineStyle = m_T_LineStyle; Style.UseParentStyleOptions = (byte)m_usedAttribute; Style.VerticalAlignment = m_verAlign; Style.WrapText = m_wrapText; return Style; } public EnumColours DiagLineColour { set { m_D_Colour = value; m_xf.DiagLineColour = (byte)value; m_styleUpdated = true; } get { return m_D_Colour; } } public EnumLineStyle DiagLineStyle { set { m_D_LineStyle = value; m_xf.DiagLineStyle = (byte)value; m_styleUpdated = true; if (m_D_LineStyle != EnumLineStyle.None) if (m_D_Colour == 0) m_D_Colour = EnumColours.SkyBlue; } get { return m_D_LineStyle; } } public bool DiagLineTopLeftToBottomRight { set { m_D_TLBR = value; m_xf.DiagLineTopLeftToBottonRight = (byte)(value ? 1 : 0); m_styleUpdated = true; } get { return m_D_TLBR; } } public bool DiagLineTopRightToBottomLeft { set { m_D_TRBL = value; m_xf.DiagLineTopRightToBottonLeft = (byte)(value ? 1 : 0); m_styleUpdated = true; } get { return m_D_TRBL; } } //public ExcelCellStyle(ExcelWorkbook pWorkbook):this() //{ // this.pWorkbook = pWorkbook; //} internal ExcelCellStyle() { this.pWorkbook = pWorkbook; m_font = new ExcelFont(); m_format = new ExcelFormat(); m_xf = new Biff8Excel.Records.ExtendedFormat(); m_celLocked = true; m_verAlign = EnumVerticalAlignment.Bottom; m_fillBackColour = EnumColours.SystemBackground; m_fillForeColour = EnumColours.SystemBorder; m_xf.CellIsLocked = m_celLocked; m_xf.VerticalAlignment = (byte)m_verAlign; m_xf.PatternBackColour = (byte)m_fillBackColour; m_xf.PatternForeColour = (byte)m_fillForeColour; //m_formatIndex = 0; m_styleIndex = -1; // This is the default font; //m_fontIndex = 0; m_font.Init(); } public ExcelFont Font { set { m_font = value; } get { return m_font; } } public ExcelFormat Format { set { m_format = value; } get { return m_format; } } internal ushort FontIndex { set { m_fontIndex = value; m_xf.FontIndex = value; m_styleUpdated = true; } get { return m_fontIndex; } } internal ushort FormatIndex { set { m_formatIndex = value; m_xf.FormatIndex = value; // user defined formats start at 164 m_styleUpdated = true; } get { return m_formatIndex; } } public bool FormulaHidden { set { m_formulaHidden = value; m_xf.FormulaHidden = value; m_styleUpdated = true; } get { return m_formulaHidden; } } public EnumHorizontalAlignment HorizontalAlignment { set { m_horizAlign = value; m_xf.HorizontalAlignment = (byte)value; m_styleUpdated = true; } get { return m_horizAlign; } } public void RowsInit() { m_verAlign = EnumVerticalAlignment.Bottom; m_fillForeColour = EnumColours.SystemBorder; m_fillBackColour = EnumColours.SystemBackground; m_celLocked = true; m_styleType = EnumType.Cell; } public EnumColours LeftLineColour { set { m_L_Colour = value; m_xf.LeftLineColour = (byte)value; m_styleUpdated = true; } get { return m_L_Colour; } } public EnumLineStyle LeftLineStyle { set { m_L_LineStyle = value; m_xf.LeftLineStyle = (byte)value; m_styleUpdated = true; if (m_L_LineStyle != EnumLineStyle.None) if (m_L_Colour == 0) m_L_Colour = EnumColours.SkyBlue; } get { return m_L_LineStyle; } } internal bool Match(ExcelCellStyle RHS) { //if (this.Equals(RHS)) if (m_fontIndex == RHS.FontIndex && m_formatIndex == RHS.FormatIndex && m_celLocked == RHS.CellIsLocked && m_formulaHidden == RHS.FormulaHidden && m_styleType == RHS.StyleType && m_horizAlign == RHS.HorizontalAlignment && m_wrapText == RHS.WrapText && m_verAlign == RHS.VerticalAlignment && m_RotatAngle == RHS.TextRotationAngle && m_stackText == RHS.StackText && m_L_LineStyle == RHS.LeftLineStyle && m_T_LineStyle == RHS.TopLineStyle && m_R_LineStyle == RHS.RightLineStyle && m_B_LineStyle == RHS.BottomLineStyle && m_L_Colour == RHS.LeftLineColour && m_T_Colour == RHS.TopLineColour && m_R_Colour == RHS.RightLineColour && m_B_Colour == RHS.BottomLineColour && m_Bdr_Colour == RHS.BorderColour && m_Bdr_LineStyle == RHS.BorderLineStyle && m_fillPattern == RHS.Pattern && m_fillForeColour == RHS.PatternForeColour && m_fillBackColour == RHS.PatternBackColour && m_D_Colour == RHS.DiagLineColour && m_D_LineStyle == RHS.DiagLineStyle && m_D_TLBR == RHS.DiagLineTopLeftToBottomRight && m_D_TRBL == RHS.DiagLineTopRightToBottomLeft) return true; else return false; } public EnumFill Pattern { set { m_fillPattern = value; m_xf.FillPattern = (byte)value; m_styleUpdated = true; } get { return m_fillPattern; } } public EnumColours PatternBackColour { set { m_fillBackColour = value; m_xf.PatternBackColour = (byte)value; m_styleUpdated = true; } get { return m_fillBackColour; } } public EnumColours PatternForeColour { set { m_fillForeColour = value; m_xf.PatternForeColour = (byte)value; m_styleUpdated = true; } get { return m_fillForeColour; } } public EnumColours RightLineColour { set { m_R_Colour = value; m_xf.RightLineColour = (byte)value; m_styleUpdated = true; } get { return m_R_Colour; } } public EnumLineStyle RightLineStyle { set { m_R_LineStyle = value; m_xf.RightLineStyle = (byte)value; m_styleUpdated = true; if (m_R_LineStyle != EnumLineStyle.None) if (m_R_Colour == 0) m_R_Colour = EnumColours.SkyBlue; } get { return m_R_LineStyle; } } private bool FindFontIndex(ExcelFont ef) { if (ef.Match(m_font)) return true; else return false; } private bool FindFormatIndex(ExcelFormat ef) { if (ef.Match(m_format)) return true; else return false; } private bool FindStyleIndex(ExcelCellStyle ef) { if (ef.Match(this)) return true; else return false; } /// /// check if the font used in this style exists /// if not create one and place the index in the cell style /// /// internal ushort GetXFIndex() { int count; if (m_fontIndex == 0) { count = pWorkbook.Fonts.FindIndex(FindFontIndex); if (count != -1) { if (count >= 4) count++; this.FontIndex = (ushort)count; } else this.FontIndex = pWorkbook.CreateNewFont(m_font); } if (m_formatIndex == 0 && this.Format.FormatString.Length != 0) { ExcelFormat ef = pWorkbook.Formats.Find(FindFormatIndex); if (ef != null) this.FormatIndex = ef.index; else this.FormatIndex = pWorkbook.CreateNewFormat(m_format); } if (m_styleIndex == -1) { count = pWorkbook.Styles.FindIndex(FindStyleIndex); if (count != -1) m_styleIndex = (ushort)count; else m_styleIndex = pWorkbook.CreateNewStyle(this); } return (ushort)m_styleIndex; } public bool StackText { set { m_stackText = value; m_xf.StackText = value; m_styleUpdated = true; } get {return m_stackText; } } public EnumType StyleType { set { m_styleType = value; m_xf.StyleType = (byte)value; m_styleUpdated = true; } get { return m_styleType; } } public bool StyleUpdated { // Check for updates to the font or format get { //m_styleUpdated = false; if (m_font.FontUpdated || m_format.Formatupdated) m_styleUpdated = true; return m_styleUpdated; } } public byte TextRotationAngle { set { if (value > 180) return; m_RotatAngle = value; m_xf.TextRoationAngle = value; m_styleUpdated = true; } get { return m_RotatAngle; } } public EnumLineStyle TopLineStyle { set { m_T_LineStyle = value; m_xf.TopLineStyle = (byte)value; m_styleUpdated = true; if (m_T_LineStyle != EnumLineStyle.None) if (m_T_Colour == 0) m_T_Colour = EnumColours.SkyBlue; } get { return m_T_LineStyle; } } public EnumColours TopLineColour { set { m_T_Colour = value; m_xf.TopLineColour = (byte)value; m_styleUpdated = true; } get { return m_T_Colour; } } internal byte UseParentStyleOptions { set { m_usedAttribute = (EnumUsedAttrib)value; m_xf.UsedAttribute = value; m_styleUpdated = true; } } public EnumVerticalAlignment VerticalAlignment { set { m_verAlign = value; m_xf.VerticalAlignment = (byte)value; m_styleUpdated = true; } get { return m_verAlign; } } public bool WrapText { set { m_wrapText = value; m_xf.WrapText = value; m_styleUpdated = true; } get { return m_wrapText; } } internal byte[] WriteRecord() { return m_xf.GetByte(); } #region IDisposable ³ΙΤ± public void Dispose() { m_xf = null; m_font.Dispose(); m_font = null; m_format.Dispose(); m_format = null; } #endregion } }