Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing font (Trebuchet MS, Calibari) in Excel programmatically C#

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.

like image 348
Sathish Avatar asked Sep 02 '10 13:09

Sathish


2 Answers

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
like image 137
Tomek Szpakowicz Avatar answered Sep 29 '22 16:09

Tomek Szpakowicz


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.

like image 28
Lukas Avatar answered Sep 29 '22 18:09

Lukas