Exemple #1
0
        public CustomStylesheet()
        {
            // blank font list
            var fonts = new Fonts();
            fonts.AppendChild(new Font());
            fonts.Count = 1;
            Append(fonts);

            // create fills
            var fills = new Fills();

            // create a solid blue fill
            var solidBlue = new PatternFill() { PatternType = PatternValues.Solid };
            solidBlue.ForegroundColor = new ForegroundColor { Rgb = HexBinaryValue.FromString("397FDB") }; // blue fill
            solidBlue.BackgroundColor = new BackgroundColor { Indexed = 64 };

            fills.AppendChild(new Fill { PatternFill = new PatternFill { PatternType = PatternValues.None } }); // required, reserved by Excel
            fills.AppendChild(new Fill { PatternFill = new PatternFill { PatternType = PatternValues.Gray125 } }); // required, reserved by Excel
            fills.AppendChild(new Fill { PatternFill = solidBlue });
            fills.Count = 3;
            Append(fills);

            // blank border list
            var borders = new Borders();
            borders.AppendChild(new Border());
            borders.AppendChild(new Border()
            {
                TopBorder = new TopBorder() { Style = BorderStyleValues.Thin },
                RightBorder = new RightBorder() { Style = BorderStyleValues.Thin },
                BottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin },
                LeftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }
            });
            borders.Count = 2;
            Append(borders);

            // blank cell format list
            var cellStyleFormats = new CellStyleFormats();
            cellStyleFormats.AppendChild(new CellFormat());
            cellStyleFormats.Count = 1;
            Append(cellStyleFormats);

            // cell format list
            var cellFormats = new CellFormats();
            // empty one for index 0, seems to be required
            cellFormats.AppendChild(new CellFormat());
            // cell format default with border
            cellFormats.AppendChild(new CellFormat() { FormatId = 0, FontId = 0, BorderId = 1, FillId = 0 }).AppendChild(new Alignment() { WrapText = true });
            // cell format for header (blue with border)
            cellFormats.AppendChild(new CellFormat { FormatId = 0, FontId = 0, BorderId = 1, FillId = 2, ApplyFill = true }).AppendChild(new Alignment { Horizontal = HorizontalAlignmentValues.Center });
            cellFormats.Count = 2;
            Append(cellFormats);
        }
Exemple #2
0
        private static CellStyleFormats CreateCellStyleFormats()
        {
            var csfs = new CellStyleFormats();

            // cell style 0
            csfs.AppendChild(new CellFormat
            {
                NumberFormatId = 0,
                FontId         = 0,
                FillId         = 0,
                BorderId       = 0
            });
            csfs.Count = UInt32Value.FromUInt32((uint)csfs.ChildElements.Count);
            return(csfs);
        }
Exemple #3
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);
        }
Exemple #4
0
        private static void GenerateWorkbookStylesPartContent(WorkbookStylesPart workbookStylesPart)
        {
            Stylesheet styleSheet = new Stylesheet()
            {
                MCAttributes = new MarkupCompatibilityAttributes()
                {
                    Ignorable = "x14ac x16r2 xr"
                }
            };

            //this namespace are revelead using the took OPEN XML PRODUCTIVITY TOOL -- REFLECT CODE feature
            styleSheet.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            styleSheet.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
            styleSheet.AddNamespaceDeclaration("x16r2", "http://schemas.microsoft.com/office/spreadsheetml/2015/02/main");
            styleSheet.AddNamespaceDeclaration("xr", "http://schemas.microsoft.com/office/spreadsheetml/2014/revision");

            #region fonts

            Fonts fontList = new Fonts {
                Count = 3
            };

            Font  f1     = new Font();
            Color color1 = new Color()
            {
                Rgb = HexBinaryValue.FromString("FF000000")
            };
            f1.Append(color1);

            Font     f2        = new Font();
            FontSize fontSize2 = new FontSize()
            {
                Val = 12D
            };
            Color color2 = new Color()
            {
                Rgb = HexBinaryValue.FromString("FF808080")
            };
            Bold     bold2     = new Bold();
            Italic   it2       = new Italic();
            FontName fontName2 = new FontName()
            {
                Val = "Arial Black"
            };
            FontFamilyNumbering fontFamilyNumbering2 = new FontFamilyNumbering()
            {
                Val = 2
            };

            f2.Append(fontSize2);
            f2.Append(color2);
            f2.Append(fontName2);
            f2.Append(bold2);
            f2.Append(it2);
            f2.Append(fontFamilyNumbering2);

            Font     f3        = new Font();
            FontSize fontSize3 = new FontSize()
            {
                Val = 16D
            };
            Color color3 = new Color()
            {
                Rgb = HexBinaryValue.FromString("FF0000FF")
            };
            Underline ud3       = new Underline();
            FontName  fontName3 = new FontName()
            {
                Val = "Times New Roman"
            };
            FontFamilyNumbering fontFamilyNumbering3 = new FontFamilyNumbering()
            {
                Val = 1
            };


            f3.Append(fontSize3);
            f3.Append(color3);
            f3.Append(ud3);
            f3.Append(fontName3);
            f3.Append(fontFamilyNumbering3);

            fontList.Append(f1);
            fontList.Append(f2);
            fontList.Append(f3);
            #endregion

            #region Fills

            Fills fillList = new Fills();

            //solid red fill
            var solidRed = new PatternFill()
            {
                PatternType = PatternValues.Solid
            };
            solidRed.ForegroundColor = new ForegroundColor {
                Rgb = HexBinaryValue.FromString("FFFF0000")
            };
            solidRed.BackgroundColor = new BackgroundColor {
                Indexed = 64
            };

            fillList.AppendChild(new Fill {
                PatternFill = new PatternFill {
                    PatternType = PatternValues.None
                }
            });                                                                                                    // required, reserved by Excel
            fillList.AppendChild(new Fill {
                PatternFill = new PatternFill {
                    PatternType = PatternValues.Gray125
                }
            });                                                                                                       // required, reserved by Excel
            fillList.AppendChild(new Fill {
                PatternFill = solidRed
            });
            fillList.Count = 3;
            #endregion


            #region Borders
            Borders bordersList = new Borders
            {
                Count = 1
            };

            Border         border1         = new Border();
            LeftBorder     leftBorder1     = new LeftBorder();
            RightBorder    rightBorder1    = new RightBorder();
            TopBorder      topBorder1      = new TopBorder();
            BottomBorder   bottomBorder1   = new BottomBorder();
            DiagonalBorder diagonalBorder1 = new DiagonalBorder();

            border1.Append(leftBorder1);
            border1.Append(rightBorder1);
            border1.Append(topBorder1);
            border1.Append(bottomBorder1);
            border1.Append(diagonalBorder1);

            bordersList.Append(border1);


            Border border2 = new Border();

            LeftBorder leftBorder2 = new LeftBorder()
            {
                Style = BorderStyleValues.Thick
            };
            Color colorborder = new Color()
            {
                Indexed = (UInt32Value)64U
            };

            leftBorder2.Append(colorborder);

            RightBorder rightBorder2 = new RightBorder()
            {
                Style = BorderStyleValues.Thin
            };
            Color color4 = new Color()
            {
                Indexed = (UInt32Value)64U
            };

            rightBorder2.Append(color4);

            TopBorder topBorder2 = new TopBorder()
            {
                Style = BorderStyleValues.Thick
            };
            Color color5 = new Color()
            {
                Indexed = (UInt32Value)64U
            };

            topBorder2.Append(color5);

            BottomBorder bottomBorder2 = new BottomBorder()
            {
                Style = BorderStyleValues.Thin
            };
            Color color6 = new Color()
            {
                Indexed = (UInt32Value)64U
            };

            bottomBorder2.Append(color6);
            DiagonalBorder diagonalBorder2 = new DiagonalBorder();

            border2.Append(leftBorder2);
            border2.Append(rightBorder2);
            border2.Append(topBorder2);
            border2.Append(bottomBorder2);
            border2.Append(diagonalBorder2);

            bordersList.Append(border2);

            #endregion

            #region Cellformats



            // blank cell format list
            CellStyleFormats blankcellStyleFormatList = new CellStyleFormats
            {
                Count = 1
            };
            blankcellStyleFormatList.AppendChild(new CellFormat());

            // cell format list
            CellFormats cellStyleFormatList = new CellFormats();

            // empty one for index 0, seems to be required
            cellStyleFormatList.AppendChild(new CellFormat());

            // cell format references style format 0, font 0, border 0, fill 2 and applies the fill
            cellStyleFormatList.AppendChild(new CellFormat {
                FormatId = 0, FontId = 0, BorderId = 1, FillId = 2, ApplyFill = true
            }).AppendChild(new Alignment {
                Horizontal = HorizontalAlignmentValues.Center
            });
            cellStyleFormatList.AppendChild(new CellFormat {
                FormatId = 0, FontId = 1, BorderId = 1, FillId = 0, ApplyFill = true
            });
            cellStyleFormatList.AppendChild(new CellFormat {
                FormatId = 0, FontId = 2, BorderId = 1, FillId = 0, ApplyFill = true
            });
            cellStyleFormatList.Count = 4;

            #endregion

            styleSheet.Append(fontList);
            styleSheet.Append(fillList);
            styleSheet.Append(bordersList);
            styleSheet.Append(blankcellStyleFormatList);
            styleSheet.Append(cellStyleFormatList);

            workbookStylesPart.Stylesheet = styleSheet;
        }