6

I am currently working in a C# application which has a class which will generate an excel file. Everything went smooth. The data populated on the excel sheet has 'Times New Roman' has font. I would like to change it to some other fonts (Calibari). How can I do that programmatically.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Sathish
  • 869
  • 2
  • 13
  • 28

8 Answers8

17

From what I tried, simply changing font name, size etc... on range changes font for that range:

range.Font.Name = "Arial"
range.Font.Size = 10
range.Font.Bold = true
Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55
7

Here is how:

    //Declare Excel Interop variables
    Microsoft.Office.Interop.Excel.Application xlApp;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

    //Initialize variables
    xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    //Set global attributes
    xlApp.StandardFont = "Arial Narrow";
    xlApp.StandardFontSize = 10;

Focus on the 2nd line from the bottom. That sets the default font type, but I wanted to show you where xlApp came from, even if it's self explanatory.

Lukas
  • 2,885
  • 2
  • 29
  • 31
4

the following worked for me, when I tried setting the default application font it did nothing so I was able to set the font name of the active sheet rows and it worked. Also worth noting I used and tested this using Excel Interop version 12

Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Create\Add workbook object
Excel.Workbooks workBooks = excelApp.Workbooks;
//Excel.Workbook
Excel.Workbook workBook = workBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
//use worksheet object 
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
//set default font
workSheet.Rows.Font.Name = "Arial"; 
Jpsh
  • 1,697
  • 12
  • 17
3

Have you tried something like this:

new Font("Arial", 10, FontStyle.Bold);
Orca
  • 2,035
  • 4
  • 24
  • 39
3
var range = worksheet.get_Range(string.Format("{0}:{0}", startRowIndex, Type.Missing));
            range = range.EntireRow;
            range.Style.Font.Name = "Arial";
            range.Style.Font.Bold = false;
            range.Style.Font.Size = 12;
1

Hey Do not upset I do it and works for me .

Just define Font.Name and excell sheet fill all sheet use everywhere . Any Way Code is :

 workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount-1]].Merge();
                workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Interior.Color = ColorTranslator.ToOle(Color.FromArgb(23,65,59));
                workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Style.Font.Name = "Arial Narrow";
bluenesman
  • 61
  • 5
1

((Excel.Range)WorksheetResult.UsedRange).Font.Name = "Avant Garde";

WorksheetResult is just a sheet reference.

gorlaz
  • 468
  • 6
  • 20
0

Found this thread by my own similar problem. I had a little picker box that, when a cell was clicked, needed to paste a unique font's symbol into the selected excel cell. Here's how i did that:

string selectedItem = arrayOfSymbols[tableLayoutPanel1.GetRow((Control)sender), tableLayoutPanel1.GetColumn((Control)sender)];

Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range cell = Globals.ThisAddIn.Application.ActiveCell;

ws.Cells[cell.Row, cell.Column].Font.Name = "My Custom Font";
ws.Cells[cell.Row, cell.Column] = selectedItem;
ty1
  • 334
  • 4
  • 19