Ejemplo n.º 1
0
        internal void LoadStylesheet()
        {
            countStyle = 0;
            listStyle = new List<string>();
            dictStyleHash = new Dictionary<string, int>();

            NumberFormatGeneralId = -1;
            NumberFormatGeneralText = SLConstants.NumberFormatGeneral;
            NextNumberFormatId = SLConstants.CustomNumberFormatIdStartIndex;
            dictStyleNumberingFormat = new Dictionary<int, string>();
            dictStyleNumberingFormatHash = new Dictionary<string, int>();

            countStyleFont = 0;
            listStyleFont = new List<string>();
            dictStyleFontHash = new Dictionary<string, int>();

            countStyleFill = 0;
            listStyleFill = new List<string>();
            dictStyleFillHash = new Dictionary<string, int>();

            countStyleBorder = 0;
            listStyleBorder = new List<string>();
            dictStyleBorderHash = new Dictionary<string, int>();

            countStyleCellStyle = 0;
            listStyleCellStyle = new List<string>();
            dictStyleCellStyleHash = new Dictionary<string, int>();

            countStyleCellStyleFormat = 0;
            listStyleCellStyleFormat = new List<string>();
            dictStyleCellStyleFormatHash = new Dictionary<string, int>();

            countStyleDifferentialFormat = 0;
            listStyleDifferentialFormat = new List<string>();
            dictStyleDifferentialFormatHash = new Dictionary<string, int>();

            countStyleTableStyle = 0;
            listStyleTableStyle = new List<string>();
            dictStyleTableStyleHash = new Dictionary<string, int>();

            int i = 0;
            string sHash = string.Empty;

            if (wbp.WorkbookStylesPart != null)
            {
                WorkbookStylesPart wbsp = wbp.WorkbookStylesPart;

                this.NextNumberFormatId = SLConstants.CustomNumberFormatIdStartIndex;
                this.StylesheetColors = null;

                using (OpenXmlReader oxr = OpenXmlReader.Create(wbsp))
                {
                    while (oxr.Read())
                    {
                        if (oxr.ElementType == typeof(NumberingFormats))
                        {
                            NumberingFormat nf;
                            using (OpenXmlReader oxrNF = OpenXmlReader.Create((NumberingFormats)oxr.LoadCurrentElement()))
                            {
                                while (oxrNF.Read())
                                {
                                    if (oxrNF.ElementType == typeof(NumberingFormat))
                                    {
                                        nf = (NumberingFormat)oxrNF.LoadCurrentElement();
                                        if (nf.NumberFormatId != null && nf.FormatCode != null)
                                        {
                                            i = (int)nf.NumberFormatId.Value;
                                            sHash = nf.FormatCode.Value;
                                            dictStyleNumberingFormat[i] = sHash;
                                            dictStyleNumberingFormatHash[sHash] = i;

                                            if (sHash.Equals(SLConstants.NumberFormatGeneral, StringComparison.InvariantCultureIgnoreCase))
                                            {
                                                this.NumberFormatGeneralText = sHash;
                                                this.NumberFormatGeneralId = i;
                                            }

                                            // if there's a number format greater than the next number,
                                            // obviously we want to increment.
                                            // if there's a current number equal to the next number,
                                            // we want to increment because this number exists!
                                            // Emphasis on *next* number
                                            if (i >= this.NextNumberFormatId)
                                            {
                                                this.NextNumberFormatId = this.NextNumberFormatId + 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (oxr.ElementType == typeof(Fonts))
                        {
                            SLFont fontSL;
                            using (OpenXmlReader oxrFont = OpenXmlReader.Create((Fonts)oxr.LoadCurrentElement()))
                            {
                                while (oxrFont.Read())
                                {
                                    if (oxrFont.ElementType == typeof(Font))
                                    {
                                        fontSL = new SLFont(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                                        fontSL.FromFont((Font)oxrFont.LoadCurrentElement());
                                        this.ForceSaveToStylesheetFont(fontSL.ToHash());
                                    }
                                }
                            }
                            countStyleFont = listStyleFont.Count;
                        }
                        else if (oxr.ElementType == typeof(Fills))
                        {
                            SLFill fillSL;
                            using (OpenXmlReader oxrFill = OpenXmlReader.Create((Fills)oxr.LoadCurrentElement()))
                            {
                                while (oxrFill.Read())
                                {
                                    if (oxrFill.ElementType == typeof(Fill))
                                    {
                                        fillSL = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                                        fillSL.FromFill((Fill)oxrFill.LoadCurrentElement());
                                        this.ForceSaveToStylesheetFill(fillSL.ToHash());
                                    }
                                }
                            }
                            countStyleFill = listStyleFill.Count;
                        }
                        else if (oxr.ElementType == typeof(Borders))
                        {
                            SLBorder borderSL;
                            using (OpenXmlReader oxrBorder = OpenXmlReader.Create((Borders)oxr.LoadCurrentElement()))
                            {
                                while (oxrBorder.Read())
                                {
                                    if (oxrBorder.ElementType == typeof(Border))
                                    {
                                        borderSL = new SLBorder(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                                        borderSL.FromBorder((Border)oxrBorder.LoadCurrentElement());
                                        this.ForceSaveToStylesheetBorder(borderSL.ToHash());
                                    }
                                }
                            }
                            countStyleBorder = listStyleBorder.Count;
                        }
                        else if (oxr.ElementType == typeof(CellStyleFormats))
                        {
                            SLStyle styleSL;
                            using (OpenXmlReader oxrCellStyleFormats = OpenXmlReader.Create((CellStyleFormats)oxr.LoadCurrentElement()))
                            {
                                while (oxrCellStyleFormats.Read())
                                {
                                    if (oxrCellStyleFormats.ElementType == typeof(CellFormat))
                                    {
                                        styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                                        styleSL.FromCellFormat((CellFormat)oxrCellStyleFormats.LoadCurrentElement());
                                        this.TranslateStyleIdsToStyles(ref styleSL);
                                        this.ForceSaveToStylesheetCellStylesFormat(styleSL.ToHash());
                                    }
                                }
                            }
                            countStyleCellStyleFormat = listStyleCellStyleFormat.Count;
                        }
                        else if (oxr.ElementType == typeof(CellFormats))
                        {
                            SLStyle styleSL;
                            using (OpenXmlReader oxrCellFormats = OpenXmlReader.Create((CellFormats)oxr.LoadCurrentElement()))
                            {
                                while (oxrCellFormats.Read())
                                {
                                    if (oxrCellFormats.ElementType == typeof(CellFormat))
                                    {
                                        styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                                        styleSL.FromCellFormat((CellFormat)oxrCellFormats.LoadCurrentElement());
                                        this.TranslateStyleIdsToStyles(ref styleSL);
                                        this.ForceSaveToStylesheet(styleSL.ToHash());
                                    }
                                }
                            }
                            countStyle = listStyle.Count;
                        }
                        else if (oxr.ElementType == typeof(CellStyles))
                        {
                            SLCellStyle csSL;
                            using (OpenXmlReader oxrCellStyles = OpenXmlReader.Create((CellStyles)oxr.LoadCurrentElement()))
                            {
                                while (oxrCellStyles.Read())
                                {
                                    if (oxrCellStyles.ElementType == typeof(CellStyle))
                                    {
                                        csSL = new SLCellStyle();
                                        csSL.FromCellStyle((CellStyle)oxrCellStyles.LoadCurrentElement());
                                        this.ForceSaveToStylesheetCellStyle(csSL.ToHash());
                                    }
                                }
                            }
                            countStyleCellStyle = listStyleCellStyle.Count;
                        }
                        else if (oxr.ElementType == typeof(DifferentialFormats))
                        {
                            SLDifferentialFormat dfSL;
                            using (OpenXmlReader oxrDiff = OpenXmlReader.Create((DifferentialFormats)oxr.LoadCurrentElement()))
                            {
                                while (oxrDiff.Read())
                                {
                                    if (oxrDiff.ElementType == typeof(DifferentialFormat))
                                    {
                                        dfSL = new SLDifferentialFormat();
                                        dfSL.FromDifferentialFormat((DifferentialFormat)oxrDiff.LoadCurrentElement());
                                        this.ForceSaveToStylesheetDifferentialFormat(dfSL.ToHash());
                                    }
                                }
                            }
                            countStyleDifferentialFormat = listStyleDifferentialFormat.Count;
                        }
                        else if (oxr.ElementType == typeof(TableStyles))
                        {
                            TableStyles tss = (TableStyles)oxr.LoadCurrentElement();
                            SLTableStyle tsSL;
                            i = 0;
                            using (OpenXmlReader oxrTableStyles = OpenXmlReader.Create(tss))
                            {
                                while (oxrTableStyles.Read())
                                {
                                    if (oxrTableStyles.ElementType == typeof(TableStyle))
                                    {
                                        tsSL = new SLTableStyle();
                                        tsSL.FromTableStyle((TableStyle)oxrTableStyles.LoadCurrentElement());
                                        sHash = tsSL.ToHash();
                                        listStyleTableStyle.Add(sHash);
                                        dictStyleTableStyleHash[sHash] = i;
                                        ++i;
                                    }
                                }
                            }
                            countStyleTableStyle = listStyleTableStyle.Count;

                            if (tss.DefaultTableStyle != null)
                            {
                                this.TableStylesDefaultTableStyle = tss.DefaultTableStyle.Value;
                            }
                            else
                            {
                                this.TableStylesDefaultTableStyle = string.Empty;
                            }

                            if (tss.DefaultPivotStyle != null)
                            {
                                this.TableStylesDefaultPivotStyle = tss.DefaultPivotStyle.Value;
                            }
                            else
                            {
                                this.TableStylesDefaultPivotStyle = string.Empty;
                            }
                        }
                        else if (oxr.ElementType == typeof(Colors))
                        {
                            this.StylesheetColors = (Colors)(oxr.LoadCurrentElement().CloneNode(true));
                        }
                    }
                }

                // Force a "General" number format to be saved.
                // Upper case is used by LibreOffice. Is it case insensitive?
                if (this.NumberFormatGeneralId < 0)
                {
                    if (!dictStyleNumberingFormat.ContainsKey(0))
                    {
                        this.NumberFormatGeneralId = 0;
                        this.NumberFormatGeneralText = SLConstants.NumberFormatGeneral;
                        dictStyleNumberingFormat[this.NumberFormatGeneralId] = this.NumberFormatGeneralText;
                        dictStyleNumberingFormatHash[this.NumberFormatGeneralText] = this.NumberFormatGeneralId;
                    }
                    else
                    {
                        this.NumberFormatGeneralId = this.NextNumberFormatId;
                        this.NumberFormatGeneralText = SLConstants.NumberFormatGeneral;
                        dictStyleNumberingFormat[this.NumberFormatGeneralId] = this.NumberFormatGeneralText;
                        dictStyleNumberingFormatHash[this.NumberFormatGeneralText] = this.NumberFormatGeneralId;

                        ++this.NextNumberFormatId;
                    }
                }

                if (listStyleFont.Count == 0)
                {
                    SLFont fontSL = new SLFont(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    fontSL.SetFont(FontSchemeValues.Minor, SLConstants.DefaultFontSize);
                    fontSL.SetFontThemeColor(SLThemeColorIndexValues.Dark1Color);
                    this.SaveToStylesheetFont(fontSL.ToHash());
                }

                if (listStyleFill.Count == 0)
                {
                    SLFill fillNone = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    fillNone.SetPatternType(PatternValues.None);
                    this.SaveToStylesheetFill(fillNone.ToHash());

                    SLFill fillGray125 = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    fillGray125.SetPatternType(PatternValues.Gray125);
                    this.SaveToStylesheetFill(fillGray125.ToHash());
                }
                else
                {
                    // make sure there's at least a "none" pattern
                    SLFill fillNone = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    fillNone.SetPatternType(PatternValues.None);
                    this.SaveToStylesheetFill(fillNone.ToHash());
                }

                // make sure there's at least an empty border
                SLBorder borderEmpty = new SLBorder(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                this.SaveToStylesheetBorder(borderEmpty.ToHash());

                int iCanonicalCellStyleFormatId = 0;
                SLStyle style = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                style.FormatCode = this.NumberFormatGeneralText;
                style.fontReal.FromHash(listStyleFont[0]);
                style.Fill.SetPatternType(PatternValues.None);
                style.Border = new SLBorder(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);

                // there's at least one cell style format
                iCanonicalCellStyleFormatId = this.SaveToStylesheetCellStylesFormat(style.ToHash());

                // there's at least one style
                style.CellStyleFormatId = (uint)iCanonicalCellStyleFormatId;
                this.SaveToStylesheet(style.ToHash());

                if (listStyleCellStyle.Count == 0)
                {
                    SLCellStyle csNormal = new SLCellStyle();
                    csNormal.Name = "Normal";
                    //csNormal.FormatId = 0;
                    csNormal.FormatId = (uint)iCanonicalCellStyleFormatId;
                    csNormal.BuiltinId = 0;
                    this.SaveToStylesheetCellStyle(csNormal.ToHash());
                }
            }
            else
            {
                // no numbering format by default
                this.NextNumberFormatId = SLConstants.CustomNumberFormatIdStartIndex;

                this.NumberFormatGeneralId = 0;
                this.NumberFormatGeneralText = SLConstants.NumberFormatGeneral;
                dictStyleNumberingFormat[this.NumberFormatGeneralId] = this.NumberFormatGeneralText;
                dictStyleNumberingFormatHash[this.NumberFormatGeneralText] = this.NumberFormatGeneralId;

                SLFont fontDefault = new SLFont(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                fontDefault.SetFont(FontSchemeValues.Minor, SLConstants.DefaultFontSize);
                fontDefault.SetFontThemeColor(SLThemeColorIndexValues.Dark1Color);
                this.SaveToStylesheetFont(fontDefault.ToHash());

                SLFill fillNone = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                fillNone.SetPatternType(PatternValues.None);
                this.SaveToStylesheetFill(fillNone.ToHash());

                SLFill fillGray125 = new SLFill(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                fillGray125.SetPatternType(PatternValues.Gray125);
                this.SaveToStylesheetFill(fillGray125.ToHash());

                SLBorder borderEmpty = new SLBorder(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                this.SaveToStylesheetBorder(borderEmpty.ToHash());

                int iCanonicalCellStyleFormatId = 0;
                SLStyle style = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                style.FormatCode = this.NumberFormatGeneralText;
                style.Font = fontDefault;
                style.Fill = fillNone;
                style.Border = borderEmpty;
                iCanonicalCellStyleFormatId = this.SaveToStylesheetCellStylesFormat(style.ToHash());

                style.CellStyleFormatId = (uint)iCanonicalCellStyleFormatId;
                this.SaveToStylesheet(style.ToHash());

                SLCellStyle csNormal = new SLCellStyle();
                csNormal.Name = "Normal";
                //csNormal.FormatId = 0;
                csNormal.FormatId = (uint)iCanonicalCellStyleFormatId;
                csNormal.BuiltinId = 0;
                this.SaveToStylesheetCellStyle(csNormal.ToHash());

                this.TableStylesDefaultTableStyle = SLConstants.DefaultTableStyle;
                this.TableStylesDefaultPivotStyle = SLConstants.DefaultPivotStyle;
            }
        }