Ejemplo n.º 1
0
        public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
        {
            using (var Reader = zip.GetXmlReader(@"xl/styles.xml"))
            {
                if (!Reader.IsStartElement("styleSheet", NsSpreadsheetMl))
                {
                    return;
                }
                if (!XmlReaderHelper.ReadFirstContent(Reader))
                {
                    return;
                }
                while (!Reader.EOF)
                {
                    if (Reader.IsStartElement("cellXfs", NsSpreadsheetMl))
                    {
                        if (!XmlReaderHelper.ReadFirstContent(Reader))
                        {
                            return;
                        }

                        var index = 0;
                        while (!Reader.EOF)
                        {
                            if (Reader.IsStartElement("xf", NsSpreadsheetMl))
                            {
                                int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
                                int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
                                _cellXfs.Add(index, new StyleRecord()
                                {
                                    XfId = xfId, NumFmtId = numFmtId
                                });
                                Reader.Skip();
                                index++;
                            }
                            else if (!XmlReaderHelper.SkipContent(Reader))
                            {
                                break;
                            }
                        }
                    }
                    else if (Reader.IsStartElement("cellStyleXfs", NsSpreadsheetMl))
                    {
                        if (!XmlReaderHelper.ReadFirstContent(Reader))
                        {
                            return;
                        }

                        var index = 0;
                        while (!Reader.EOF)
                        {
                            if (Reader.IsStartElement("xf", NsSpreadsheetMl))
                            {
                                int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
                                int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);

                                _cellStyleXfs.Add(index, new StyleRecord()
                                {
                                    XfId = xfId, NumFmtId = numFmtId
                                });
                                Reader.Skip();
                                index++;
                            }
                            else if (!XmlReaderHelper.SkipContent(Reader))
                            {
                                break;
                            }
                        }
                    }
                    else if (!XmlReaderHelper.SkipContent(Reader))
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
        {
            using (var Reader = zip.GetXmlReader(@"xl/styles.xml"))
            {
                if (!XmlReaderHelper.IsStartElement(Reader, "styleSheet", _ns))
                {
                    return;
                }
                if (!XmlReaderHelper.ReadFirstContent(Reader))
                {
                    return;
                }
                while (!Reader.EOF)
                {
                    if (XmlReaderHelper.IsStartElement(Reader, "cellXfs", _ns))
                    {
                        if (!XmlReaderHelper.ReadFirstContent(Reader))
                        {
                            continue;
                        }

                        var index = 0;
                        while (!Reader.EOF)
                        {
                            if (XmlReaderHelper.IsStartElement(Reader, "xf", _ns))
                            {
                                int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
                                int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
                                _cellXfs.Add(index, new StyleRecord()
                                {
                                    XfId = xfId, NumFmtId = numFmtId
                                });
                                Reader.Skip();
                                index++;
                            }
                            else if (!XmlReaderHelper.SkipContent(Reader))
                            {
                                break;
                            }
                        }
                    }
                    else if (XmlReaderHelper.IsStartElement(Reader, "cellStyleXfs", _ns))
                    {
                        if (!XmlReaderHelper.ReadFirstContent(Reader))
                        {
                            continue;
                        }

                        var index = 0;
                        while (!Reader.EOF)
                        {
                            if (XmlReaderHelper.IsStartElement(Reader, "xf", _ns))
                            {
                                int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
                                int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);

                                _cellStyleXfs.Add(index, new StyleRecord()
                                {
                                    XfId = xfId, NumFmtId = numFmtId
                                });
                                Reader.Skip();
                                index++;
                            }
                            else if (!XmlReaderHelper.SkipContent(Reader))
                            {
                                break;
                            }
                        }
                    }
                    else if (XmlReaderHelper.IsStartElement(Reader, "numFmts", _ns))
                    {
                        if (!XmlReaderHelper.ReadFirstContent(Reader))
                        {
                            continue;
                        }

                        while (!Reader.EOF)
                        {
                            if (XmlReaderHelper.IsStartElement(Reader, "numFmt", _ns))
                            {
                                int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
                                var formatCode = Reader.GetAttribute("formatCode");


                                //TODO: determine the type according to the format
                                var type = typeof(string);
                                if (DateTimeHelper.IsDateTimeFormat(formatCode))
                                {
                                    type = typeof(DateTime?);
                                }

                                _customFormats.Add(numFmtId, new NumberFormatString(formatCode, type));
                                Reader.Skip();
                            }
                            else if (!XmlReaderHelper.SkipContent(Reader))
                            {
                                break;
                            }
                        }
                    }
                    else if (!XmlReaderHelper.SkipContent(Reader))
                    {
                        break;
                    }
                }
            }
        }