Changing font size of one cell in excel using C#

前端 未结 4 604
孤街浪徒
孤街浪徒 2020-12-19 11:34

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). <

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 11:52

    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);
       ...
    }
    

提交回复
热议问题