internal void WriteStylesheet()
        {
            int i = 0;

            SLStyle styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
            for (i = 0; i < listStyle.Count; ++i)
            {
                styleSL.FromHash(listStyle[i]);
                if (styleSL.nfFormatCode.FormatCode.Length > 0)
                {
                    this.SaveToStylesheetNumberingFormat(styleSL.nfFormatCode.ToHash());
                }

                if (styleSL.HasFont)
                {
                    this.SaveToStylesheetFont(styleSL.fontReal.ToHash());
                }

                if (styleSL.HasFill)
                {
                    this.SaveToStylesheetFill(styleSL.fillReal.ToHash());
                }

                if (styleSL.HasBorder)
                {
                    this.SaveToStylesheetBorder(styleSL.borderReal.ToHash());
                }
            }

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

                if (dictStyleNumberingFormat.Count > 0)
                {
                    if (dictStyleNumberingFormat.Count == 1
                        && dictStyleNumberingFormatHash.ContainsKey(SLConstants.NumberFormatGeneral)
                        && dictStyleNumberingFormatHash[SLConstants.NumberFormatGeneral] == 0)
                    {
                        // it just contains our "default" number format, so don't do anything.
                    }
                    else
                    {
                        wbsp.Stylesheet.NumberingFormats = new NumberingFormats();
                        List<int> listKeys = dictStyleNumberingFormat.Keys.ToList<int>();
                        listKeys.Sort();
                        if (listKeys[0] == 0
                            && dictStyleNumberingFormat[listKeys[0]].Equals(SLConstants.NumberFormatGeneral, StringComparison.InvariantCultureIgnoreCase))
                        {
                            listKeys.RemoveAt(0);
                        }
                        wbsp.Stylesheet.NumberingFormats.Count = (uint)listKeys.Count;
                        SLNumberingFormat nfSL;
                        for (i = 0; i < listKeys.Count; ++i)
                        {
                            nfSL = new SLNumberingFormat();
                            nfSL.FromHash(dictStyleNumberingFormat[listKeys[i]]);
                            wbsp.Stylesheet.NumberingFormats.Append(new NumberingFormat()
                            {
                                NumberFormatId = (uint)listKeys[i],
                                FormatCode = SLTool.XmlWrite(nfSL.FormatCode)
                            });
                        }
                    }
                }

                if (listStyleFont.Count > countStyleFont)
                {
                    if (wbsp.Stylesheet.Fonts == null)
                    {
                        wbsp.Stylesheet.Fonts = new Fonts();
                    }
                    wbsp.Stylesheet.Fonts.Count = (uint)listStyleFont.Count;
                    diff = listStyleFont.Count - countStyleFont;
                    for (i = 0; i < diff; ++i)
                    {
                        wbsp.Stylesheet.Fonts.Append(new Font() { InnerXml = listStyleFont[i + countStyleFont] });
                    }
                }

                if (listStyleFill.Count > countStyleFill)
                {
                    if (wbsp.Stylesheet.Fills == null)
                    {
                        wbsp.Stylesheet.Fills = new Fills();
                    }
                    wbsp.Stylesheet.Fills.Count = (uint)listStyleFill.Count;
                    diff = listStyleFill.Count - countStyleFill;
                    for (i = 0; i < diff; ++i)
                    {
                        wbsp.Stylesheet.Fills.Append(new Fill() { InnerXml = listStyleFill[i + countStyleFill] });
                    }
                }

                if (listStyleBorder.Count > countStyleBorder)
                {
                    if (wbsp.Stylesheet.Borders == null)
                    {
                        wbsp.Stylesheet.Borders = new Borders();
                    }
                    wbsp.Stylesheet.Borders.Count = (uint)listStyleBorder.Count;
                    diff = listStyleBorder.Count - countStyleBorder;
                    SLBorder borderSL = new SLBorder(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    for (i = 0; i < diff; ++i)
                    {
                        borderSL.FromHash(listStyleBorder[i + countStyleBorder]);
                        wbsp.Stylesheet.Borders.Append(borderSL.ToBorder());
                    }
                }

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

                if (listStyleCellStyleFormat.Count > countStyleCellStyleFormat)
                {
                    if (wbsp.Stylesheet.CellStyleFormats == null)
                    {
                        wbsp.Stylesheet.CellStyleFormats = new CellStyleFormats();
                    }
                    wbsp.Stylesheet.CellStyleFormats.Count = (uint)listStyleCellStyleFormat.Count;
                    diff = listStyleCellStyleFormat.Count - countStyleCellStyleFormat;
                    styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    for (i = 0; i < diff; ++i)
                    {
                        styleSL.FromHash(listStyleCellStyleFormat[i + countStyleCellStyleFormat]);
                        this.TranslateStylesToStyleIds(ref styleSL);
                        wbsp.Stylesheet.CellStyleFormats.Append(styleSL.ToCellFormat());
                    }
                }

                if (listStyle.Count > countStyle)
                {
                    if (wbsp.Stylesheet.CellFormats == null)
                    {
                        wbsp.Stylesheet.CellFormats = new CellFormats();
                    }
                    wbsp.Stylesheet.CellFormats.Count = (uint)listStyle.Count;
                    diff = listStyle.Count - countStyle;
                    styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);
                    for (i = 0; i < diff; ++i)
                    {
                        styleSL.FromHash(listStyle[i + countStyle]);
                        this.TranslateStylesToStyleIds(ref styleSL);
                        styleSL.CellStyleFormatId = (uint)iCanonicalCellStyleFormatId;
                        if (styleSL.NumberFormatId != styleCanonical.NumberFormatId) styleSL.ApplyNumberFormat = true;
                        if (styleSL.FontId != styleCanonical.FontId) styleSL.ApplyFont = true;
                        if (styleSL.FillId != styleCanonical.FillId) styleSL.ApplyFill = true;
                        if (styleSL.BorderId != styleCanonical.BorderId) styleSL.ApplyBorder = true;
                        if (styleSL.HasAlignment) styleSL.ApplyAlignment = true;
                        if (styleSL.HasProtection) styleSL.ApplyProtection = true;
                        wbsp.Stylesheet.CellFormats.Append(styleSL.ToCellFormat());
                    }
                }

                if (listStyleCellStyle.Count > countStyleCellStyle)
                {
                    if (wbsp.Stylesheet.CellStyles == null)
                    {
                        wbsp.Stylesheet.CellStyles = new CellStyles();
                    }
                    wbsp.Stylesheet.CellStyles.Count = (uint)listStyleCellStyle.Count;
                    diff = listStyleCellStyle.Count - countStyleCellStyle;
                    SLCellStyle csSL = new SLCellStyle();
                    for (i = 0; i < diff; ++i)
                    {
                        csSL.FromHash(listStyleCellStyle[i + countStyleCellStyle]);
                        wbsp.Stylesheet.CellStyles.Append(csSL.ToCellStyle());
                    }
                }

                if (listStyleDifferentialFormat.Count > countStyleDifferentialFormat)
                {
                    if (wbsp.Stylesheet.DifferentialFormats == null)
                    {
                        wbsp.Stylesheet.DifferentialFormats = new DifferentialFormats();
                    }
                    wbsp.Stylesheet.DifferentialFormats.Count = (uint)listStyleDifferentialFormat.Count;
                    diff = listStyleDifferentialFormat.Count - countStyleDifferentialFormat;
                    for (i = 0; i < diff; ++i)
                    {
                        wbsp.Stylesheet.DifferentialFormats.Append(new DifferentialFormat() { InnerXml = listStyleDifferentialFormat[i + countStyleDifferentialFormat] });
                    }
                }

                if (listStyleTableStyle.Count > countStyleTableStyle)
                {
                    if (wbsp.Stylesheet.TableStyles == null)
                    {
                        wbsp.Stylesheet.TableStyles = new TableStyles();
                    }
                    wbsp.Stylesheet.TableStyles.Count = (uint)listStyleTableStyle.Count;
                    if (this.TableStylesDefaultTableStyle.Length > 0)
                    {
                        wbsp.Stylesheet.TableStyles.DefaultTableStyle = this.TableStylesDefaultTableStyle;
                    }
                    if (this.TableStylesDefaultPivotStyle.Length > 0)
                    {
                        wbsp.Stylesheet.TableStyles.DefaultPivotStyle = this.TableStylesDefaultPivotStyle;
                    }
                    diff = listStyleTableStyle.Count - countStyleTableStyle;
                    SLTableStyle tsSL = new SLTableStyle();
                    for (i = 0; i < diff; ++i)
                    {
                        tsSL.FromHash(listStyleTableStyle[i + countStyleTableStyle]);
                        wbsp.Stylesheet.TableStyles.Append(tsSL.ToTableStyle());
                    }
                }

                // we're not touching Colors here, so there shouldn't be anything to update
            }
            else
            {
                WorkbookStylesPart wbsp = wbp.AddNewPart<WorkbookStylesPart>();
                using (MemoryStream ms = new MemoryStream())
                {
                    using (StreamWriter sw = new StreamWriter(ms))
                    {
                        sw.Write("<x:styleSheet xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");

                        if (dictStyleNumberingFormat.Count > 0)
                        {
                            if (dictStyleNumberingFormat.Count == 1 
                                && dictStyleNumberingFormatHash.ContainsKey(SLConstants.NumberFormatGeneral)
                                && dictStyleNumberingFormatHash[SLConstants.NumberFormatGeneral] == 0)
                            {
                                // it just contains our "default" number format, so don't do anything.
                            }
                            else
                            {
                                List<int> listKeys = dictStyleNumberingFormat.Keys.ToList<int>();
                                listKeys.Sort();
                                if (listKeys[0] == 0
                                    && dictStyleNumberingFormat[listKeys[0]].Equals(SLConstants.NumberFormatGeneral, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    listKeys.RemoveAt(0);
                                }
                                sw.Write("<x:numFmts count=\"{0}\">", listKeys.Count);
                                for (i = 0; i < listKeys.Count; ++i)
                                {
                                    sw.Write("<x:numFmt numFmtId=\"{0}\" formatCode=\"{1}\" />", listKeys[i], SLTool.XmlWrite(dictStyleNumberingFormat[listKeys[i]]));
                                }
                                sw.Write("</x:numFmts>");
                            }
                        }

                        sw.Write("<x:fonts count=\"{0}\">", listStyleFont.Count);
                        for (i = 0; i < listStyleFont.Count; ++i)
                        {
                            sw.Write("<x:font>{0}</x:font>", listStyleFont[i]);
                        }
                        sw.Write("</x:fonts>");

                        sw.Write("<x:fills count=\"{0}\">", listStyleFill.Count);
                        for (i = 0; i < listStyleFill.Count; ++i)
                        {
                            sw.Write("<x:fill>{0}</x:fill>", listStyleFill[i]);
                        }
                        sw.Write("</x:fills>");

                        List<System.Drawing.Color> listempty = new List<System.Drawing.Color>();

                        sw.Write("<x:borders count=\"{0}\">", listStyleBorder.Count);
                        SLBorder slb;
                        for (i = 0; i < listStyleBorder.Count; ++i)
                        {
                            slb = new SLBorder(listempty, listempty);
                            slb.FromHash(listStyleBorder[i]);
                            slb.Sync();

                            sw.Write("<x:border");
                            if (slb.DiagonalUp != null) sw.Write(" diagonalUp=\"{0}\"", slb.DiagonalUp.Value ? "1" : "0");
                            if (slb.DiagonalDown != null) sw.Write(" diagonalDown=\"{0}\"", slb.DiagonalDown.Value ? "1" : "0");
                            if (slb.Outline != null && !slb.Outline.Value) sw.Write(" outline=\"0\"");
                            sw.Write(">");

                            // by "default" always have left, right, top, bottom and diagonal borders, even if empty?
                            sw.Write(SLBorderProperties.WriteToXmlTag("left", slb.LeftBorder));
                            sw.Write(SLBorderProperties.WriteToXmlTag("right", slb.RightBorder));
                            sw.Write(SLBorderProperties.WriteToXmlTag("top", slb.TopBorder));
                            sw.Write(SLBorderProperties.WriteToXmlTag("bottom", slb.BottomBorder));
                            sw.Write(SLBorderProperties.WriteToXmlTag("diagonal", slb.DiagonalBorder));
                            if (slb.HasVerticalBorder) sw.Write(SLBorderProperties.WriteToXmlTag("vertical", slb.VerticalBorder));
                            if (slb.HasHorizontalBorder) sw.Write(SLBorderProperties.WriteToXmlTag("horizontal", slb.HorizontalBorder));

                            sw.Write("</x:border>");
                        }
                        sw.Write("</x:borders>");

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

                        sw.Write("<x:cellStyleXfs count=\"{0}\">", listStyleCellStyleFormat.Count);
                        for (i = 0; i < listStyleCellStyleFormat.Count; ++i)
                        {
                            styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, listempty, listempty);
                            styleSL.FromHash(listStyleCellStyleFormat[i]);
                            this.TranslateStylesToStyleIds(ref styleSL);
                            sw.Write(styleSL.WriteToXmlTag());
                        }
                        sw.Write("</x:cellStyleXfs>");

                        sw.Write("<x:cellXfs count=\"{0}\">", listStyle.Count);
                        for (i = 0; i < listStyle.Count; ++i)
                        {
                            styleSL = new SLStyle(SimpleTheme.MajorLatinFont, SimpleTheme.MinorLatinFont, listempty, listempty);
                            styleSL.FromHash(listStyle[i]);
                            this.TranslateStylesToStyleIds(ref styleSL);
                            styleSL.CellStyleFormatId = (uint)iCanonicalCellStyleFormatId;
                            if (styleSL.NumberFormatId != styleCanonical.NumberFormatId) styleSL.ApplyNumberFormat = true;
                            if (styleSL.FontId != styleCanonical.FontId) styleSL.ApplyFont = true;
                            if (styleSL.FillId != styleCanonical.FillId) styleSL.ApplyFill = true;
                            if (styleSL.BorderId != styleCanonical.BorderId) styleSL.ApplyBorder = true;
                            if (styleSL.HasAlignment) styleSL.ApplyAlignment = true;
                            if (styleSL.HasProtection) styleSL.ApplyProtection = true;
                            sw.Write(styleSL.WriteToXmlTag());
                        }
                        sw.Write("</x:cellXfs>");

                        sw.Write("<x:cellStyles count=\"{0}\">", listStyleCellStyle.Count);
                        SLCellStyle csSL;
                        for (i = 0; i < listStyleCellStyle.Count; ++i)
                        {
                            csSL = new SLCellStyle();
                            csSL.FromHash(listStyleCellStyle[i]);
                            sw.Write(csSL.WriteToXmlTag());
                        }
                        sw.Write("</x:cellStyles>");

                        sw.Write("<x:dxfs count=\"{0}\">", listStyleDifferentialFormat.Count);
                        for (i = 0; i < listStyleDifferentialFormat.Count; ++i)
                        {
                            sw.Write("<x:dxf>{0}</x:dxf>", listStyleDifferentialFormat[i]);
                        }
                        sw.Write("</x:dxfs>");

                        sw.Write("<x:tableStyles count=\"{0}\"", listStyleTableStyle.Count);
                        if (this.TableStylesDefaultTableStyle.Length > 0)
                        {
                            sw.Write(" defaultTableStyle=\"{0}\"", this.TableStylesDefaultTableStyle);
                        }
                        if (this.TableStylesDefaultPivotStyle.Length > 0)
                        {
                            sw.Write(" defaultPivotStyle=\"{0}\"", this.TableStylesDefaultPivotStyle);
                        }
                        sw.Write(">");
                        SLTableStyle tsSL;
                        for (i = 0; i < listStyleTableStyle.Count; ++i)
                        {
                            tsSL = new SLTableStyle();
                            tsSL.FromHash(listStyleTableStyle[i]);
                            sw.Write(tsSL.WriteToXmlTag());
                        }
                        sw.Write("</x:tableStyles>");

                        sw.Write("</x:styleSheet>");

                        sw.Flush();
                        ms.Position = 0;
                        wbsp.FeedData(ms);
                    }
                }
                // end of writing new stylesheet
            }
        }