Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make excel work sheet header row bold using OpenXML

I am using openXML, Asp.net and c# to create an Excel workbook, I have a requirement that to make Header row of all sheets should be 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;
    }

It is making all the cells bold, I am missing the code that apply this to a particular row/cells

Thanks in Advance, AR

like image 811
Aruns Avatar asked Apr 28 '15 07:04

Aruns


People also ask

How do I make the header row bold with OpenXml in Excel?

According to your description, you want to set the font to be bold in the header row of a spreadsheet by Open XML SDK. To edit the font of cell in Open XML SDK, we need to add a new Font element and related CellFormat element under WorkbookStylesPart. Stylesheet element in the spreadsheet.

How do I make text bold in OpenXml?

You need to use separate Run elements for the differently styled pieces of text. You can add the bold by creating a RunProperties element and adding a Bold element to that.

How do I bold the first row in Excel in C#?

You must pass value of row 0 so that first row of your excel sheets have column headers with bold font size. Just change DataColumnCollection to your columns name and change col. Caption to specific column name. You may do this to cell of excel sheet you want bold.

What is OpenXml Excel?

Office Open XML (also informally known as OOXML) is a zipped, XML-based file format developed by Microsoft for representing spreadsheets, charts, presentations and word processing documents.


1 Answers

I got the Answer from another post. 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;
}
like image 176
Aruns Avatar answered Oct 23 '22 01:10

Aruns