Click here to Skip to main content
Rate this: bad
good
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
Edited 22-Jan-13 0:50am
lukeer15.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

xlWorkSheet.get_Range("A1", "D1").Font.Bold = true;
Please try the above code.
  Permalink  
v2
Rate this: bad
good
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;
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
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