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

I am currently working in a C # application that has a class that will generate an excel file. Everything went smoothly. The data filled in the Excel worksheet is in Times New Roman font. I would like to change it to some other fonts (Calibari). How can I do this programmatically.

+3
source share
7 answers

Here's 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 second line from the bottom. This sets the default font type, but I wanted to show you where xlApp came from, even if it explains itself.

+6
source

From what I tried, just changing the font name, size, etc ... in the range the font for that range changes:

range.Font.Name = "Arial"
range.Font.Size = 10
range.Font.Bold = true
+14

- :

new Font("Arial", 10, FontStyle.Bold);
+2
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;
+2

, , .

Font.Name excell sheet, . :

 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";
+1

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

WorksheetResult - .

+1

, , , , . , Excel Interop 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"; 
+1

Source: https://habr.com/ru/post/1762857/


All Articles