none
Open XML sdk for making font bold RRS feed

  • Question

  • Hi,

    I am using OpenXML SDK for exporting data into a an excelsheet using C#.

    I want header row in excel to be in bold.

    Please give me code for the same.

    Thanks in advance

    Monday, February 10, 2014 9:35 AM

Answers

  • Hi,

    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.

    I write a sample to set the font of first row to be bold for your reference.

    public static void SetSpreadsheetHeaderBold()
    {
        string fileName = @"C:\Users\Documents\Book1.xlsx";
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, true))
        {
    
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
    
    
            WorkbookStylesPart stylesPart = spreadsheetDocument.WorkbookPart.WorkbookStylesPart;
    
            Font font1 = new Font(
                    new Bold(),
                    new FontSize() { Val = 11 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" });
            stylesPart.Stylesheet.Fonts.Append(font1);
            stylesPart.Stylesheet.Save();
    
            UInt32Value fontId = Convert.ToUInt32(stylesPart.Stylesheet.Fonts.ChildElements.Count - 1);
            CellFormat cf = new CellFormat() { FontId = fontId, FillId = 0, BorderId = 0, ApplyFont = true };
    
            stylesPart.Stylesheet.CellFormats.Append(cf);
    
            Row r = sheetData.Elements<Row>().First<Row>();
    
            int index1 = stylesPart.Stylesheet.CellFormats.ChildElements.Count-1;
            foreach (Cell c in r.Elements<Cell>())
            {
                c.StyleIndex =  Convert.ToUInt32(index1 );
                worksheetPart.Worksheet.Save();
            }
            spreadsheetDocument.Close();
    
        }
    }




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 11, 2014 9:01 AM
    Moderator

All replies

  • Hi,

    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.

    I write a sample to set the font of first row to be bold for your reference.

    public static void SetSpreadsheetHeaderBold()
    {
        string fileName = @"C:\Users\Documents\Book1.xlsx";
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, true))
        {
    
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
    
    
            WorkbookStylesPart stylesPart = spreadsheetDocument.WorkbookPart.WorkbookStylesPart;
    
            Font font1 = new Font(
                    new Bold(),
                    new FontSize() { Val = 11 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" });
            stylesPart.Stylesheet.Fonts.Append(font1);
            stylesPart.Stylesheet.Save();
    
            UInt32Value fontId = Convert.ToUInt32(stylesPart.Stylesheet.Fonts.ChildElements.Count - 1);
            CellFormat cf = new CellFormat() { FontId = fontId, FillId = 0, BorderId = 0, ApplyFont = true };
    
            stylesPart.Stylesheet.CellFormats.Append(cf);
    
            Row r = sheetData.Elements<Row>().First<Row>();
    
            int index1 = stylesPart.Stylesheet.CellFormats.ChildElements.Count-1;
            foreach (Cell c in r.Elements<Cell>())
            {
                c.StyleIndex =  Convert.ToUInt32(index1 );
                worksheetPart.Worksheet.Save();
            }
            spreadsheetDocument.Close();
    
        }
    }




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 11, 2014 9:01 AM
    Moderator
  • Hi,

    I have a problem, i'm french and i need to use this kind of charaters : é è ç à and others like this.

    I have my font declared like that :

    Fonts fonts = new Fonts(

                         new Font
                        (
                            new FontSize() { Val = 10 }, 
                            new FontName() { Val = "Arial" }, 
                            new Bold() { Val = true }, 
                            new Color() { Rgb = "000000" }
                        )

    );

    But i have a black losange with a "?" in it a the place of the "é" character....

    Any ideas ?

    thanks in advance.

    Pierre-Michel

    Friday, September 15, 2017 5:08 PM