Click here to Skip to main content
15,877,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating an excel, where i want to make some text bold in one of the columns for which i tried with below code and its not working.

Can some one suggest me where am i doing wrong.


What I have tried:

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(m_SchemaFileName, SpreadsheetDocumentType.Workbook);
                // Add a WorkbookPart to the document.
                WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

                // Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                SheetData sheetData = new SheetData();
                worksheetPart.Worksheet = new Worksheet(sheetData);

                // Add Sheets to the Workbook.
                Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                    AppendChild<Sheets>(new Sheets());

                // Append a new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet()
                    Id = spreadsheetDocument.WorkbookPart.
                    SheetId = 1,
                    Name = "ClassMap"
                 Row row4 = new Row() { RowIndex = 4 };
                Cell header4 = new Cell() { CellReference = "B4", CellValue = new CellValue("ClassIdentifyingLabel"), DataType = CellValues.String };
                AddBold(spreadsheetDocument, header4);

public CellFormats AddCellFormat(CellFormats cf, Fonts fs)
            cf = new CellFormats();
            CellFormat cellFormat2 = new CellFormat() { NumberFormatId = 1, FontId = (UInt32)(fs.Elements<Font>().Count() - 1), FillId = 0, BorderId = 0, FormatId = 0, ApplyFill = true };
            return cf;
        public  void AddBold(SpreadsheetDocument document, Cell c)
            Fonts fs = AddFont(document.WorkbookPart.WorkbookStylesPart.Stylesheet.Fonts);
            CellFormats cf =  AddCellFormat(document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats,fs);
            c.StyleIndex = (UInt32)(cf.Elements<CellFormat>().Count() - 1);
Updated 21-Jul-22 7:59am
Maciej Los 21-Jul-22 6:44am    
I'd suggest to use EPPlus software rather then OpenXml. See: NuGet Gallery | EPPlus 6.0.6[^].

To make a cell bold use
c.StyleIndex = (UInt32Value)1U;
Share this answer
Sampath579 21-Jul-22 23:31pm    
This is not working.
jekin77 22-Jul-22 3:32am    
you can also use the ClosedXML Lib to work with Excel/OpemXML

see -
example : cell.GetRichText().AddText(" BIG ").SetBold();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900