示例#1
0
        /// <summary>
        ///  Create a number format in the stylesheet and apply to the cellFormat
        /// </summary>
        /// <param name="cellInfo"></param>
        /// <param name="formatCode"></param>
        /// <param name="stylesheet"></param>
        /// <param name="cellFormat"></param>
        private static void UpdateNumberFormat(ExcelStylesManager stylesManager, ExcelCellStyleInfo cellInfo, string formatCode, ref Stylesheet stylesheet, ref CellFormat cellFormat)
        {
            if (cellFormat == null)
            {
                throw new ArgumentNullException("cellFormat");
            }

            string numFormat = cellInfo.NumberFormat ?? formatCode;

            // Anything to apply?
            if (string.IsNullOrEmpty(numFormat))
            {
                return;
            }

            // Look up existing...
            var         item = stylesManager.numberFormats.Find(numFormat);
            UInt32Value id   = item.Value;

            if (id == null)
            {
                // Create (and append?) a new numbering format in the stylesheet
                id = stylesheet.AddNumberingFormat(cellInfo.NumberFormat ?? formatCode);
                stylesManager.numberFormats.Add(numFormat, id);
            }

            cellFormat.NumberFormatId = id;
        }
示例#2
0
        /// <summary>
        /// Tries the get or create numbering format.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        private static bool TryGetOrCreateNumberingFormat(uint index, Stylesheet source, Stylesheet target, out uint result)
        {
            if (source.NumberingFormats == null)
            {
                source.NumberingFormats = new NumberingFormats();
            }

            var match = (from nf in source.NumberingFormats.Descendants <NumberingFormat>()
                         where nf.NumberFormatId.HasValue && nf.NumberFormatId.Value == index
                         select nf).FirstOrDefault();

            if (match == null)
            {
                result = 0;
                return(false);
            }

            uint id = 0;

            //// to fix naughty use of reserved number formats
            //if (index < 164)
            //{
            //    result = target.AddNumberingFormat(match);
            //    return true;
            //}

            if (target.TryMatchNumberingFormat(match, out id))
            {
                result = id;
            }
            else
            {
                result = target.AddNumberingFormat(match);
            }
            return(true);
        }