Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In excel, how do I set the cell font to be bold programatically?

My code so far follows:
C#
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.

C#
chartRange.Cells.Font.Bold = true;
Posted
Updated 21-Jan-13 23:50pm
v2

Try chartRange.Font.Bold = true; instead of chartRange.Cells.Font.Bold = true;.
 
Share this answer
 
xlWorkSheet.get_Range("A1", "D1").Font.Bold = true;

Please try the above code.
 
Share this answer
 
v2
Here I show you all code to set excel font:
C#
//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;
 
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