예제 #1
0
        public int GetColumnStyle(string dataFormat)
        {
            var numberFormat = new NumberFormat
            {
                FormatCode = dataFormat,
                Id         = 164 + NumberFormats.Count // custom number formats start from numFmtId=164
            };

            var cellFormat = new CellFormat
            {
                FormatId = numberFormat.Id,
                Id       = CellFormats.Count
            };

            NumberFormats.Add(numberFormat);
            CellFormats.Add(cellFormat);

            return(cellFormat.Id);
        }
예제 #2
0
        /// <summary>
        /// Loads the style XML to memory
        /// </summary>
        private void LoadFromDocument()
        {
            //NumberFormats
            ExcelNumberFormatXml.AddBuildIn(NameSpaceManager, NumberFormats);
            XmlNode numNode = _styleXml.SelectSingleNode(NumberFormatsPath, _nameSpaceManager);

            if (numNode != null)
            {
                foreach (XmlNode n in numNode)
                {
                    ExcelNumberFormatXml nf = new ExcelNumberFormatXml(_nameSpaceManager, n);
                    NumberFormats.Add(nf.Id, nf);
                    if (nf.NumFmtId >= NumberFormats.NextId)
                    {
                        NumberFormats.NextId = nf.NumFmtId + 1;
                    }
                }
            }

            //Fonts
            XmlNode fontNode = _styleXml.SelectSingleNode(FontsPath, _nameSpaceManager);

            foreach (XmlNode n in fontNode)
            {
                ExcelFontXml f = new ExcelFontXml(_nameSpaceManager, n);
                Fonts.Add(f.Id, f);
            }

            //Fills
            XmlNode fillNode = _styleXml.SelectSingleNode(FillsPath, _nameSpaceManager);

            foreach (XmlNode n in fillNode)
            {
                ExcelFillXml f;
                if (n.FirstChild != null && n.FirstChild.LocalName == "gradientFill")
                {
                    f = new ExcelGradientFillXml(_nameSpaceManager, n);
                }
                else
                {
                    f = new ExcelFillXml(_nameSpaceManager, n);
                }
                Fills.Add(f.Id, f);
            }

            //Borders
            XmlNode borderNode = _styleXml.SelectSingleNode(BordersPath, _nameSpaceManager);

            foreach (XmlNode n in borderNode)
            {
                ExcelBorderXml b = new ExcelBorderXml(_nameSpaceManager, n);
                Borders.Add(b.Id, b);
            }

            //cellStyleXfs
            XmlNode styleXfsNode = _styleXml.SelectSingleNode(CellStyleXfsPath, _nameSpaceManager);

            if (styleXfsNode != null)
            {
                foreach (XmlNode n in styleXfsNode)
                {
                    ExcelXfs item = new ExcelXfs(_nameSpaceManager, n, this);
                    CellStyleXfs.Add(item.Id, item);
                }
            }

            XmlNode styleNode = _styleXml.SelectSingleNode(CellXfsPath, _nameSpaceManager);

            for (int i = 0; i < styleNode.ChildNodes.Count; i++)
            {
                XmlNode  n    = styleNode.ChildNodes[i];
                ExcelXfs item = new ExcelXfs(_nameSpaceManager, n, this);
                CellXfs.Add(item.Id, item);
            }

            //cellStyle
            XmlNode namedStyleNode = _styleXml.SelectSingleNode(CellStylesPath, _nameSpaceManager);

            if (namedStyleNode != null)
            {
                foreach (XmlNode n in namedStyleNode)
                {
                    ExcelNamedStyleXml item = new ExcelNamedStyleXml(_nameSpaceManager, n, this);
                    NamedStyles.Add(item.Name, item);
                }
            }

            //dxfsPath
            XmlNode dxfsNode = _styleXml.SelectSingleNode(dxfsPath, _nameSpaceManager);

            if (dxfsNode != null)
            {
                foreach (XmlNode x in dxfsNode)
                {
                    ExcelDxfStyleConditionalFormatting item = new ExcelDxfStyleConditionalFormatting(_nameSpaceManager, x, this);
                    Dxfs.Add(item.Id, item);
                }
            }
        }
        private void InitialNumberFormats()
        {
            NumberFormat generalFormat = new NumberFormat();

            generalFormat.Name             = "General";
            generalFormat.Format           = "G";
            generalFormat.PropertyChanged += generalFormat_PropertyChanged;
            NoDecimalPlacesList.Add(generalFormat);
            NumberFormats.Add(generalFormat);

            NumberFormat numberFormat = new NumberFormat();

            numberFormat.Name             = "Number";
            numberFormat.Format           = "N";
            numberFormat.PropertyChanged += generalFormat_PropertyChanged;
            NumberFormats.Add(numberFormat);

            NumberFormat currencyFormat = new NumberFormat();

            currencyFormat.Name             = "Currency";
            currencyFormat.Format           = "C";
            currencyFormat.PropertyChanged += generalFormat_PropertyChanged;
            NumberFormats.Add(currencyFormat);

            NumberFormat percentFormat = new NumberFormat();

            percentFormat.Name             = "Percentage";
            percentFormat.Format           = "P";
            percentFormat.PropertyChanged += generalFormat_PropertyChanged;
            NumberFormats.Add(percentFormat);

            NumberFormat exponentialFormat = new NumberFormat();

            exponentialFormat.Name             = "Scientific";
            exponentialFormat.Format           = "E";
            exponentialFormat.PropertyChanged += generalFormat_PropertyChanged;
            NumberFormats.Add(exponentialFormat);

            DateFormat dateFormat = new DateFormat();

            dateFormat.Name             = "Date";
            dateFormat.Format           = "f";
            dateFormat.PropertyChanged += generalFormat_PropertyChanged;
            NumberFormats.Add(dateFormat);
            NoDecimalPlacesList.Add(dateFormat);


            SelectedNumberFormat = generalFormat;

            var cell = _cellRange.FirstOrDefault();
            var row  = _flex.Rows[cell.Row] as ExcelRow;
            var col  = _flex.Columns[cell.Column];

            FormatBase defaultFormat = generalFormat;

            if (row != null)
            {
                var cs = row.GetCellStyle(col) as ExcelCellStyle;
                var cv = row.GetValue(col);
                defaultFormat = GetNumberFormat(cs != null ? cs.Format : null, cv);
            }
            SelectedNumberFormat = defaultFormat;
            SelectedNumberFormat.PropertyChanged += SelectedNumberFormat_PropertyChanged;

            InvalidateText();
        }