How to make excel worksheet title bar bold using OpenXML

I use openXMLAsp.net and C # to create Excel workbook, I have a requirement that the title bar of all sheets be in bold.

WorkbookStylesPart stylesPart = workbookpart.AddNewPart<WorkbookStylesPart>();
        stylesPart.Stylesheet = CreateStylesheet();
        stylesPart.Stylesheet.Save();

    }
    private static Stylesheet CreateStylesheet()
    {
        Stylesheet ss = new Stylesheet();
        Fonts fts = new Fonts();
        DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font();
        Bold fbld = new Bold();
        FontName ftn = new FontName();
        ftn.Val = "Calibri";
        DocumentFormat.OpenXml.Spreadsheet.FontSize ftsz = new DocumentFormat.OpenXml.Spreadsheet.FontSize();
        ftsz.Val = 11;
        ft.FontName = ftn;
        ft.FontSize = ftsz;
        ft.Bold = fbld;
        fts.Append(ft);
        fts.Count = (uint)fts.ChildElements.Count;
        ss.Append(fts);
        return ss;
    }

Does all the cells bold, I skip the code that applies it to a specificrow/cells

Thanks, Advance, AR

+4
source share
1 answer

I received a response from another message. Create Excel file with style tag using OpenXmlWriter SAX

 private static Stylesheet CreateStylesheet()
{
   Stylesheet ss = new Stylesheet();

        Font font0 = new Font();         // Default font

        Font font1 = new Font();         // Bold font
        Bold bold = new Bold();
        font1.Append(bold);

        Fonts fonts = new Fonts();      // <APENDING Fonts>
        fonts.Append(font0);
        fonts.Append(font1);

        // <Fills>
        Fill fill0 = new Fill();        // Default fill

        Fills fills = new Fills();      // <APENDING Fills>
        fills.Append(fill0);

        // <Borders>
        Border border0 = new Border();     // Defualt border

        Borders borders = new Borders();    // <APENDING Borders>
        borders.Append(border0);

        CellFormat cellformat0 = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory | Style ID =0

        CellFormat cellformat1 = new CellFormat(){FontId = 1};
        CellFormats cellformats = new CellFormats();
        cellformats.Append(cellformat0);
        cellformats.Append(cellformat1);


        ss.Append(fonts);
        ss.Append(fills);
        ss.Append(borders);
        ss.Append(cellformats);


        return ss;
}
0
source

Source: https://habr.com/ru/post/1585050/


All Articles