7

I am working on a project that writes data to an Excel file.

Everything is finished now, however I need a few cells with a bigger size than the rest (title, etc).

I have read about this about the internet, but I keep having the same problem: when I execute my code (see below for what I have tried), everything in the worksheet becomes larger.

What I already have tried:

worksheet.Rows[1].Cells[7].Style.Font.Size = 20; 

worksheet.get_Range("A7", "A7").Style.Font.Size = 20;

None of this seems to work; what is the correct way to increase a cell's font size?

Servy
  • 202,030
  • 26
  • 332
  • 449
Arnout
  • 657
  • 4
  • 12
  • 23
  • @Arnout You can add a new answer to this question and accept it (if you decide to stick around for another day or two). [This is encouraged.](http://meta.stackexchange.com/questions/16930/is-it-ok-to-answer-your-own-question-and-accept-it) – Zairja Jul 31 '12 at 18:42

4 Answers4

14

I had to use:

worksheet.get_Range("A7", "A7").Cells.Font.Size = 20;
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
kornbread
  • 141
  • 1
  • 3
2

If the data is consistent and will always be written to the same cells then this is the simplest solution - works well for product details / contact info type exporting

// set cell A7
worksheet.get_Range("A7", "A7").Font.Size = 20;

// set cells A7, A8
worksheet.get_Range("A7", "A8").Font.Size = 20;

// set cells A7, B7
worksheet.get_Range("A7", "B7").Font.Size = 20;

// set cells A7, A8, B7, B8
worksheet.get_Range("A7", "B8").Font.Size = 20;

If the data varies and will sometimes be written to multiple rows/columns then something like this is more simple - works well for dataset / shopping list type exporting

int RowNum;
int ColNum;

// some code to set variables

worksheet.Cells[RowNum, ColNum].Font.Size = 20; 
Wayne
  • 477
  • 9
  • 16
1

I would just use:

worksheet.Range["A7"].Style.Font.Size = 20;

edit: sorry, wrong brackets

paul
  • 21,653
  • 1
  • 53
  • 54
  • doesn't seem to work. Gives this error: "Non-invocable member 'Microsoft.Office.Interop.Excel._Worksheet.Range' cannot be used like a method." – Arnout Jul 31 '12 at 13:20
  • If I use: worksheet.get_Range("A7").Style.Font.Size = 20; it still gives my whole sheet a size of 20 – Arnout Jul 31 '12 at 13:25
1

When working with interop excel, try not to write your code with "two dots" in order to clean interop excel objects. This also helps having your code more readable. Anyway, to answer your question, and using what I have pointed out... all you have to do is:

//Declare your variables
Application excel = null;
Workbook excelworkBook = null;
Range excelCellrange = null;
Worksheet worksheet = null;
Font excelFont =null;

//start your application
excel = new Application();
try
{
   ...
   //your code goes here...
   excelCellrange = worksheet.Range[worksheet.Cells[1,7],worksheet.Cells[1,7]];
   excelFont = excelCellrange.Font;
   excelfont.Size = 20;
   ...
   ...
}
catch(Exception ex){
}
finally{
   //here put something to clean the interop objects as the link above.
   ...
   Marshal.ReleaseComObject(excelfont);
   ...
}
Community
  • 1
  • 1
Seichi
  • 273
  • 4
  • 11