Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing font size of one cell in excel using C#

Tags:

c#

excel

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?

like image 798
Arnout Avatar asked Jul 31 '12 13:07

Arnout


People also ask

How do I change the font in a single cell in Excel?

Select the cell or cell range that has the text or number you want to format. Click the arrow next to Font and pick another font.

How do I change the font size of selected cells in Excel?

To change the font size: Select the cell(s) you want to modify. On the Home tab, click the drop-down arrow next to the Font Size command, then select the desired font size. In our example, we will choose 24 to make the text larger. The text will change to the selected font size.

How do you make text bigger in a cell?

Click Home and: For a different font style, click the arrow next to the default font Calibri and pick the style you want. To increase or decrease the font size, click the arrow next to the default size 11 and pick another text size.

How do I change the font size automatically in Excel?

Under Excel, click Options. In the General category, under When creating new workbooks, do the following: In the Use this font box, click the font that you want to use. In the Font Size box, enter the font size that you want to use.


3 Answers

I had to use:

worksheet.get_Range("A7", "A7").Cells.Font.Size = 20;
like image 100
kornbread Avatar answered Nov 08 '22 12:11

kornbread


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; 
like image 43
Wayne Avatar answered Nov 08 '22 13:11

Wayne


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);
   ...
}
like image 1
Seichi Avatar answered Nov 08 '22 14:11

Seichi