示例#1
0
        /// <summary>
        /// Gets the or create cell style index inner.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <returns></returns>
        private static uint GetOrCreateCellStyleIndexInner(uint index, Stylesheet source, Stylesheet target)
        {
            var cellFormat = SafeIndex <CellFormats, CellFormat>(source.CellFormats, (int)index);

            if (cellFormat == null)
            {
                return(0);
                // this really ought to be an exception, it should never happen if i understand this correctly
                //throw new OpenXmlException(string.Format("Unable to find CellFormat with index <{0}>", index));
            }

            var cloned = (CellFormat)cellFormat.CloneNode(true);

            // get or create border if there is one
            if (cloned.BorderId != null && cloned.BorderId.HasValue)
            {
                cloned.BorderId.Value = GetOrCreateBorder(cloned.BorderId.Value, source, target);
            }

            // get or create fill if there is one
            if (cloned.FillId != null && cloned.FillId.HasValue)
            {
                cloned.FillId.Value = GetOrCreateFill(cloned.FillId.Value, source, target);
            }

            // get or create font if there is one
            if (cloned.FontId != null && cloned.FontId.HasValue)
            {
                cloned.FontId.Value = GetOrCreateFont(cloned.FontId.Value, source, target);
            }

            // get or create number format if there is one
            if (cloned.NumberFormatId != null && cloned.NumberFormatId.HasValue)
            {
                uint result = 0;
                if (TryGetOrCreateNumberingFormat(cloned.NumberFormatId.Value, source, target, out result))
                {
                    cloned.NumberFormatId.Value = result;
                }
            }

            // ?? get or create format if there is one
            //if (cloned.FormatId.HasValue){ }

            return(target.AddCellFormat(cloned));
        }
示例#2
0
        /// <summary>
        /// Creates a new style in the excel <see cref="Stylesheet"/> based on the supplied <see cref="ExportStyle"/>.<br/>
        /// Return the index of the newly created style.
        /// </summary>
        /// <param name="stylesheet"></param>
        /// <param name="cellInfo"></param>
        /// <param name="formatCode"></param>
        /// <returns></returns>
        private static uint CreateNewStyle(ExcelStylesManager stylesManager, Stylesheet stylesheet, ExcelCellStyleInfo cellInfo, string formatCode)
        {
            // Create a cellFormat to which style attributes can be applied.
            var cellFormat = new CellFormat();

            // Looks up/creates a new fill colour in the stylesheet and applies it to the cellFormat
            UpdateFillColour(stylesManager, cellInfo, ref stylesheet, ref cellFormat);

            // Create and append a new font to the stylesheet and applies it to the cellFormat
            UpdateFont(stylesManager, cellInfo, ref stylesheet, ref cellFormat);

            // Create a number format in the stylesheet and apply to the cellFormat
            UpdateNumberFormat(stylesManager, cellInfo, formatCode, ref stylesheet, ref cellFormat);

            // Creates a border in the stylesheet and applies it to the cellFormat
            UpdateBorder(stylesManager, cellInfo, ref stylesheet, ref cellFormat);

            // Create an Allignment object which manages indentation, alignment and text rotation and applies it to the cellFormat.
            UpdateTextAlignmentAndRotation(cellInfo, ref cellFormat);

            uint styleIndex = stylesheet.AddCellFormat(cellFormat);

            return(styleIndex);
        }