Making only the Header row (row1) Bold in Excel [C#]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
Given an Excel File [Tasks.xls] that I create (as a kind of template) which has a Header row [row1, the first row in the Excel File]
So obviously I want to make that rows font BOLD, problem is when I use my current code (as shown below) it seems to make the entire range BOLD so the actual rows of information I add afterwards [row2+] are all BOLD also - not just the header row as I wanted.

Code:
oSheet.get_Range("A1", "G1").Font.Bold = true;

Is there a way maybe to set the Font cell by cell? [very easy to just manually set cells [1, 1-6] bold, I just dont want every subsequent row to also be bold.
Is there a way to set it only for row1? (cell by cell basis? row by row basis? of setting Font.Bold = true?)
Thanks,
 
There a a few different ways to accomplish this.
assuming o.Sheet is the Excel.Worksheet reference.
1. using the Range properity - oSheet.Range("A1:G1").Font.Bold = True
2. using a cell by cell loop
Code:
For i = 1 to 6
oSheet.Cells(1, i).Font.Bold = True
End For
3. specific cell referencing
oSheet.Cells(1, 1).Font.Bold = True

I hope this gives you an idea how to solve your task.
 
This is how I generate my oSheet
Code:
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.Sheets["Sheet1"];

Sadly none of your 3 suggestions seem to work...
1. There is no oSheet.Range property (doesnt exist)
2/3. There is no .Cells().Font property (doesnt exist) AND it is .Cells[] not .Cells()

Any clues as to why? What I am doing wrong? Other ideas?
I find it really hard to beleive that there is no way to BOLD only certain rows or cells ...

Thanks,
 
Heres a suggestion for establishing an Excel worksheet.

Code:
Dim excelApp As New Excel.Application
If excelApp Is Nothing Then
    MessageBox.Show("Excel Installation Error", "Microsoft Excel must be installed on this computer for this feature to operate.")
End If

Cursor.Current = Cursors.AppStarting

Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
Dim row as Integer = 1

With excelWorksheet
    .Cells(row, 1).Value = "Add Something Here"
    .Cells(row, 1).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft
    .Cells(row, 1).Font.Bold = True
End With

OR

With excelWorksheet
    .Cells(row, 1).Value = "Add Something Here"
    .Cells(row, 1).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft
    .Range(row & "1:L1").Font.Bold = True you can write a function here to convert a numeric column number to a letter
End With

excelWorksheet.Columns.AutoFit()
Cursor.Current = Cursors.Default
excelApp.Visible = True

I hope this helps.
 
Back
Top