Click here to Skip to main content
12,696,179 members (30,548 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
In excel, how do I set the cell font to be bold programatically?

My code so far follows:
dr = GFun.ReadSql(sql);
xlRow = 3;
while (dr.Read())
    //ColName.Name = dr[0].ToString().Trim();
    xlWorkSheet.Cells[xlRow,1] = dr[0].ToString().Trim();
    chartRange = xlWorkSheet.get_Range("A" + xlRow, "E" + xlRow);
    chartRange.MergeCells = true;
    chartRange.Cells.Font.Bold = true;
    chartRange.Cells.Font.FontStyle = HorizontalAlignment.Center;

    xlRow = xlRow + 1;
    xlWorkSheet.Cells[xlRow, 1] = "Date";
    xlWorkSheet.Cells[xlRow, 2] = "Session 1";
    xlWorkSheet.Cells[xlRow, 3] = "Session 2";
    xlWorkSheet.Cells[xlRow, 4] = "Session 3";
    xlWorkSheet.Cells[xlRow, 5] = "Session 4";
    xlRow = xlRow + 1;

I wrote the line below but in excel it is not bold. How can I do this? From my code below, what is the mistake? Please help me.

chartRange.Cells.Font.Bold = true;
Posted 21-Jan-13 23:16pm
Updated 22-Jan-13 0:50am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try chartRange.Font.Bold = true; instead of chartRange.Cells.Font.Bold = true;.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

xlWorkSheet.get_Range("A1", "D1").Font.Bold = true;

Please try the above code.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Here I show you all code to set excel font:
//set name
sheet.Range["A3:E3"].Style.Font.FontName = "Comic Sans MS";
//set size
sheet.Range["A2:E2"].Style.Font.Size = 50;
//set excel cell data to be bold
sheet.Range["A3:E3"].Style.Font.IsBold = true;
//set excel cell data to be underline
sheet.Range["A3:E3"].Style.Font.Underline = FontUnderlineType.Single;
//set excel cell data color
sheet.Range["A4:E14"].Style.Font.IsItalic = true;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100