Changing the font size of a single cell in excel using c #

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

Everything is finished now, however I need several cells of a larger size than the rest (name, etc.).

I read about it on the Internet, but I have the same problem: when I execute my code (see below what I tried), everything on the sheet gets bigger.

What I already tried:

worksheet.Rows[1].Cells[7].Style.Font.Size = 20; worksheet.get_Range("A7", "A7").Style.Font.Size = 20; 

None of this works; What is the correct way to increase the font size of a cell?

+4
source share
4 answers

I had to use:

 worksheet.get_Range("A7", "A7").Cells.Font.Size = 20; 
+8
source

If the data is consistent and will always be recorded in the same cells, then this is the simplest solution - it works well for product information / type of contact information 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 changes and is sometimes written in several rows / columns, then something like this is simpler - it works well for exporting a data list / shopping list type

 int RowNum; int ColNum; // some code to set variables worksheet.Cells[RowNum, ColNum].Font.Size = 20; 
+1
source

When working with interop excel, try not to write two-point code for pure excel interop objects . It also helps make your code more readable. In any case, to answer your question and use what I have indicated ... 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); ... } 
+1
source

I would just use:

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

Edit: sorry, wrong brackets

0
source

All Articles