Interoperation / Office Integration Topics include: COM, P/Invoke (e.g. Win32 API), marshaling, distributed applications, VBA and interacting with office apps

Go Back  Xtreme .NET Talk > .NET > Interoperation / Office Integration > Making only the Header row (row1) Bold in Excel [C#]


Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2005, 01:49 AM
Shaitan00 Shaitan00 is offline
Junior Contributor

Preferred language:
C#
 
Join Date: Aug 2003
Location: Hell
Posts: 358
Shaitan00 is on a distinguished road
Question Making only the Header row (row1) Bold in Excel [C#]



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 don't 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,
Reply With Quote
  #2  
Old 10-02-2005, 02:38 PM
DiverDan's Avatar
DiverDan DiverDan is offline
Contributor

Preferred language:
vb.net
 
Join Date: Jan 2003
Location: Sacramento, CA
Posts: 645
DiverDan is on a distinguished road
Default

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.
__________________
Member, in good standing, of the elite fraternity of mentally challenged programmers.

Dolphins Software
Reply With Quote
  #3  
Old 10-30-2005, 11:23 AM
Shaitan00 Shaitan00 is offline
Junior Contributor

Preferred language:
C#
 
Join Date: Aug 2003
Location: Hell
Posts: 358
Shaitan00 is on a distinguished road
Default

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 (doesn't exist)
2/3. There is no .Cells().Font property (doesn't 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,
Reply With Quote
  #4  
Old 10-30-2005, 12:20 PM
DiverDan's Avatar
DiverDan DiverDan is offline
Contributor

Preferred language:
vb.net
 
Join Date: Jan 2003
Location: Sacramento, CA
Posts: 645
DiverDan is on a distinguished road
Default

Here's 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.
__________________
Member, in good standing, of the elite fraternity of mentally challenged programmers.

Dolphins Software
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting a bold grid header into my table. Tim Field ASP.NET 1 10-06-2005 12:51 PM
Making item row in ListView Bold... Lanc1988 Windows Forms 1 10-24-2004 06:13 PM
Datagrid Row Header donnacha ASP.NET 5 08-30-2003 05:15 AM
hyperlinks in header row for a datagrid? noshadows ASP.NET 1 08-11-2003 11:49 AM
Making a word bold in tooltiptext Cywizz Windows Forms 2 03-26-2003 08:05 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe