Exemplo n.º 1
0
        public static Stylesheet CreateStylesheet()
        {
            Stylesheet stylesheet1 = new Stylesheet()
            {
                MCAttributes = new MarkupCompatibilityAttributes()
                {
                    Ignorable = "x14ac"
                }
            };

            stylesheet1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            stylesheet1.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");

            var fonts1   = AddFonts();
            var fills1   = AddFills();
            var borders1 = AddBorders();

            CellStyleFormats cellStyleFormats1 = new CellStyleFormats()
            {
                Count = 1U
            };
            CellFormat cellFormat1 = new CellFormat()
            {
                NumberFormatId = 0U, FontId = 0U, FillId = 0U, BorderId = 0U
            };

            cellStyleFormats1.AppendChild(cellFormat1);

            CellFormats cellFormats1 = new CellFormats()
            {
                Count = 4U
            };
            // Black text on White background
            CellFormat cellFormat2 = new CellFormat()
            {
                NumberFormatId = 0U, FontId = 0U, FillId = 0U, BorderId = 0U, FormatId = 0U
            };
            // White text on Orange background
            CellFormat cellFormat3 = new CellFormat()
            {
                NumberFormatId = 0U, FontId = 1U, FillId = 2U, BorderId = 0U, FormatId = 0U, ApplyFill = true
            };
            // White text on Blue background
            CellFormat cellFormat4 = new CellFormat()
            {
                NumberFormatId = 0U, FontId = 1U, FillId = 3U, BorderId = 0U, FormatId = 0U, ApplyFill = true
            };
            // Black text on Yellow background
            CellFormat cellFormat5 = new CellFormat()
            {
                NumberFormatId = 0U, FontId = 0U, FillId = 4U, BorderId = 0U, FormatId = 0U, ApplyFill = true
            };

            cellFormats1.AppendChild(cellFormat2);
            cellFormats1.AppendChild(cellFormat3);
            cellFormats1.AppendChild(cellFormat4);
            cellFormats1.AppendChild(cellFormat5);

            CellStyles cellStyles1 = new CellStyles()
            {
                Count = 1U
            };
            CellStyle cellStyle1 = new CellStyle()
            {
                Name = "Normal", FormatId = 0U, BuiltinId = 0U
            };

            cellStyles1.AppendChild(cellStyle1);
            DifferentialFormats differentialFormats1 = new DifferentialFormats()
            {
                Count = 0U
            };
            TableStyles tableStyles1 = new TableStyles()
            {
                Count = 0U, DefaultTableStyle = "TableStyleMedium2", DefaultPivotStyle = "PivotStyleMedium9"
            };

            StylesheetExtensionList stylesheetExtensionList = new StylesheetExtensionList();
            StylesheetExtension     stylesheetExtension     = new StylesheetExtension()
            {
                Uri = "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}"
            };

            stylesheetExtension.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main");
            DocumentFormat.OpenXml.Office2010.Excel.SlicerStyles slicerStyles = new DocumentFormat.OpenXml.Office2010.Excel.SlicerStyles()
            {
                DefaultSlicerStyle = "SlicerStyleLight1"
            };
            stylesheetExtension.AppendChild(slicerStyles);
            stylesheetExtensionList.AppendChild(stylesheetExtension);


            stylesheet1.AppendChild(fonts1);
            stylesheet1.AppendChild(fills1);
            stylesheet1.AppendChild(borders1);
            stylesheet1.AppendChild(cellStyleFormats1);
            stylesheet1.AppendChild(cellFormats1);
            stylesheet1.AppendChild(cellStyles1);
            stylesheet1.AppendChild(differentialFormats1);
            stylesheet1.AppendChild(tableStyles1);
            stylesheet1.AppendChild(stylesheetExtensionList);
            return(stylesheet1);
        }
        private void CreateStylesheet(WorkbookPart workbookPart)
        {
            var workbookStylesPart = workbookPart.AddNewPart <WorkbookStylesPart>("rId101");

            var stylesheet = new Stylesheet {
                MCAttributes = new MarkupCompatibilityAttributes {
                    Ignorable = "x14ac"
                }
            };

            stylesheet.AddNamespaceDeclaration("mc", MC_SCHEMA);
            stylesheet.AddNamespaceDeclaration("x14ac", X14_AC_SCHEMA);

            var numberingFormats = new NumberingFormats(new NumberingFormat
            {
                NumberFormatId = 44U,
                FormatCode     = FORMAT_CODE
            })
            {
                Count = 1U
            };

            var fonts = new Fonts {
                Count = 3U, KnownFonts = true
            };

            var font1 = new Font(new FontSize {
                Val = 11D
            }, new Color {
                Theme = 1U
            }, new FontName {
                Val = "Calibri"
            },
                                 new FontFamilyNumbering {
                Val = 2
            }, new FontScheme {
                Val = FontSchemeValues.Minor
            });

            var font2 = new Font(new FontSize {
                Val = 11D
            }, new Color {
                Theme = 1U
            }, new FontName {
                Val = "Calibri"
            },
                                 new FontFamilyNumbering {
                Val = 2
            }, new FontScheme {
                Val = FontSchemeValues.Minor
            });

            var font3 = new Font(new Bold(), new FontSize {
                Val = 11D
            }, new Color {
                Theme = 0U
            },
                                 new FontName {
                Val = "Calibri"
            }, new FontFamilyNumbering {
                Val = 2
            },
                                 new FontScheme {
                Val = FontSchemeValues.Minor
            });

            fonts.Append(font1, font2, font3);

            var fills = new Fills {
                Count = 3U
            };

            var fill1 = new Fill(new PatternFill {
                PatternType = PatternValues.None
            });

            var fill2 = new Fill(new PatternFill {
                PatternType = PatternValues.Gray125
            });

            var fill3 = new Fill(new PatternFill(
                                     new ForegroundColor {
                Rgb = "FF0070C0"
            },
                                     new BackgroundColor {
                Indexed = 64U
            })
            {
                PatternType = PatternValues.Solid
            });

            fills.Append(fill1, fill2, fill3);

            var borders = new Borders(new Border(), new LeftBorder(), new RightBorder(), new TopBorder(),
                                      new BottomBorder(), new DiagonalBorder())
            {
                Count = 1U
            };

            var cellStyleFormats = new CellStyleFormats {
                Count = 2U
            };
            var cellFormat1 = new CellFormat {
                NumberFormatId = 0U, FontId = 0U, FillId = 0U, BorderId = 0U
            };
            var cellFormat2 = new CellFormat
            {
                NumberFormatId  = 44U,
                FontId          = 1U,
                FillId          = 0U,
                BorderId        = 0U,
                ApplyFont       = false,
                ApplyFill       = false,
                ApplyBorder     = false,
                ApplyAlignment  = false,
                ApplyProtection = false
            };

            cellStyleFormats.Append(cellFormat1, cellFormat2);

            var cellFormats = new CellFormats {
                Count = 6U
            };
            var cellFormat3 = new CellFormat
            {
                NumberFormatId = 0U,
                FontId         = 0U,
                FillId         = 0U,
                BorderId       = 0U,
                FormatId       = 0U
            };

            var cellFormat4 = new CellFormat
            {
                NumberFormatId  = 0U,
                FontId          = 0U,
                FillId          = 0U,
                BorderId        = 0U,
                FormatId        = 0U,
                ApplyProtection = true
            };

            var protection1 = new Protection {
                Locked = false
            };

            cellFormat4.AppendChild(protection1);

            var cellFormat5 = new CellFormat
            {
                NumberFormatId  = 44U,
                FontId          = 0U,
                FillId          = 0U,
                BorderId        = 0U,
                FormatId        = 1U,
                ApplyFont       = true,
                ApplyProtection = true
            };

            var protection2 = new Protection {
                Locked = false
            };

            cellFormat5.AppendChild(protection2);

            var cellFormat6 = new CellFormat
            {
                NumberFormatId    = 14U,
                FontId            = 0U,
                FillId            = 0U,
                BorderId          = 0U,
                FormatId          = 0U,
                ApplyNumberFormat = true,
                ApplyProtection   = true
            };

            var protection3 = new Protection {
                Locked = false
            };

            cellFormat6.AppendChild(protection3);
            var cellFormat7 = new CellFormat
            {
                NumberFormatId  = 0U,
                FontId          = 2U,
                FillId          = 2U,
                BorderId        = 0U,
                FormatId        = 0U,
                ApplyFont       = true,
                ApplyFill       = true,
                ApplyProtection = true
            };

            var cellFormat8 = new CellFormat
            {
                NumberFormatId  = 0U,
                FontId          = 0U,
                FillId          = 0U,
                BorderId        = 0U,
                FormatId        = 0U,
                ApplyProtection = true
            };

            cellFormats.Append(cellFormat3, cellFormat4, cellFormat5, cellFormat6, cellFormat7, cellFormat8);

            var cellStyles = new CellStyles {
                Count = 2U
            };
            var cellStyle1 = new CellStyle {
                Name = "Currency", FormatId = 1U, BuiltinId = 4U
            };
            var cellStyle2 = new CellStyle {
                Name = "Normal", FormatId = 0U, BuiltinId = 0U
            };

            cellStyles.Append(cellStyle1, cellStyle2);

            var differentialFormats = new DifferentialFormats {
                Count = 0U
            };

            var tableStyles = new TableStyles
            {
                Count             = 0U,
                DefaultTableStyle = "TableStyleMedium2",
                DefaultPivotStyle = "PivotStyleLight16"
            };

            var stylesheetExtensionList = new StylesheetExtensionList();

            var stylesheetExtension = new StylesheetExtension {
                Uri = "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}"
            };

            stylesheetExtension.AddNamespaceDeclaration("x14",
                                                        X14_SCHEMA);

            var slicerStyles = new X14.SlicerStyles {
                DefaultSlicerStyle = "SlicerStyleLight1"
            };

            stylesheetExtension.AppendChild(slicerStyles);

            stylesheetExtensionList.AppendChild(stylesheetExtension);

            stylesheet.Append(numberingFormats, fonts, fills, borders, cellStyleFormats, cellFormats, cellStyles,
                              differentialFormats, tableStyles, stylesheetExtensionList);

            workbookStylesPart.Stylesheet = stylesheet;
        }