How to Set Excel Font Style by Using C#

Microsoft Excel provides lots of font styles for setting text font in cells. User can choose the most favorite font style for setting text or numbers. Because of font style, the appearance of Excel file will be more wonderful. Also, sometimes, users may set different font styles for different cells. For example, the font style of header row is Impact to emphasize, while data body font style is Times New Roman. 

Besides, font size and color are contained in font style settings. Bigger size and bright color can tell readers that the data information is special or very important. 

In this post, I will introduce a method about font format settings, including font type, size, color etc. This method is based on a C# .NET Excel component, Spire.XLS for .NET. Therefore, I have add its dll file as reference in project at the beginning. 

The following code shows details about how to set Excel font style. Firstly, create a new workbook and initialize worksheet. Secondly, add text in specified cells to tell readers which style I will set and set style for text. Finally, save and launch this workbook.

Coding:

            //Create Workbook and Worksheet
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Set Font Type as Calibri
            sheet.Range[“B1”].Text = “Calibri”;
            sheet.Range[“B1”].Style.Font.FontName = “Calibri”;

            //Set Font Size as 22
            sheet.Range[“B2”].Text = “Large size”;
            sheet.Range[“B2”].Style.Font.Size = 22;

            //Set Font as Bold
            sheet.Range[“B3”].Text = “Bold”;
            sheet.Range[“B3”].Style.Font.IsBold = true;

            //Set Font as Italic
            sheet.Range[“B4”].Text = “Italic”;
            sheet.Range[“B4”].Style.Font.IsItalic = true;

            //Set Font as Superscript
            sheet.Range[“B5”].Text = “Superscript”;
            sheet.Range[“B5”].Style.Font.IsSuperscript = true;

            //Set Font as Colored
            sheet.Range[“B6”].Text = “Colored”;
            sheet.Range[“B6”].Style.Font.Color = Color.FromArgb(255, 125, 125);

            //Set Font as Underlined
            sheet.Range[“B7”].Text = “Underline”;
            sheet.Range[“B7”].Style.Font.Underline = FontUnderlineType.Single;

            //Save File
            workbook.SaveToFile(“FontStyle.xls”);

            //Launch File
            System.Diagnostics.Process.Start(“FontStyle.xls”);

Result Shown as Following:

Download Spire.XLS for .NET Here

About Delegate in .NET 4.0

I got this post from http://sunnywang.blog.com/2011/10/26/translation-about-delegate-in-net-4-0/. I think that this post is helpful for .NET learning, so I share it with you. Also, I have gotten the right to sharing it from blog's owner.

*****************************************

Delegate in .NET has the similar function to Method-Pointer in C or C++. We can call delegate to complete one function as calling method or return result of one type. But, delegate is more improved. It is data interface, including pointers to call object and method. The defined delegate in .NET needs to inherit from MulticastDelegate, which includes several called methods.

1. Delegate Definition

The following C# code is used to define delegate.

public delegate void DoSomething(int times);

There are five parts of delegate definition.

   1) public:  delegate accessibility
   2) delegate: define one delegate
   3) void: return value of delegate definition method
   4) DoSomething: delegate name
   5) (int times) parameter list of delegate method. The list includes ref parameter, out parameter, and parms variable amount parameter. Note: if there are several called method, it just can return the calculated value of the last delegate method which is run successfully by using out parameter.

It is easy to define delegate in C#. We just need to add a delegate keyword at font of method definition return value.

As is known, all the users defined delegates are inherit from MulticastDelegate. Because it is a class, so customized delegate is a class as well. The following code can verify our judgement.

.class public auto ansi sealed delegates.DoSomething
 extends [mscorlib]System.MulticastDelegate
{
 // Methods
 .method public hidebysig specialname rtspecialname
  instance void .ctor (
   object 'object',
   native int 'method'
  ) runtime managed
 {
 } // end of method DoSomething::.ctor

 .method public hidebysig newslot virtual
  instance void Invoke (
   int32 times
  ) runtime managed
 {

 } // end of method DoSomething::Invoke


 .method public hidebysig newslot virtual
  instance class [mscorlib]System.IAsyncResult BeginInvoke (
   int32 times,
   class [mscorlib]System.AsyncCallback callback,
   object 'object'
  ) runtime managed
 {

 

 } // end of method DoSomething::BeginInvoke


 .method public hidebysig newslot virtual
  instance void EndInvoke (
   class [mscorlib]System.IAsyncResult result
  ) runtime managed
 {

 } // end of method DoSomething::EndInvoke

} // end of class delegates.DoSomething

 

2. Delegate Usage

There are three types of delegate in .NET, method, anonymous method and lambda expression. The following code shows how to use delegate by method definition.

using System;

namespace delegates
{
 public delegate void DoSomething(int times);

    class Program
    {
        static void Main(string[] args)
        {
            //Declare Delegate Variable and Assign Value
            DoSomething @do = DoA;
            //Use + or += to Add Method for Delegate
            @do += DoB;
            //Run Methods of Delegate According to Sequence which We Add Delegate
            @do(1);
            //Use - or -= to Remove Delegate
            @do -= DoA;
            @do(2);

            @do -= DoB;
            //Delegate Can Be Called after Removing All the Methods as well.
            @do(3);

            Console.Read();
        }
       //Define a Method to Delegate the Same Parameter and Return Value
        static void DoA(int times)
        {
            Console.WriteLine("Do A {0}", times);
        }


        //Define a Method to Delegate the Same Parameter and Return Value
        static void DoB(int times)
        {
            Console.WriteLine("Do B {0}", times);
        }
    }
}

In the above code, we define variable @do of DoSomething and assign value to it with DoA method directly. Then, use + or += to add another method for the delegate. Also, we can remove methods by using - or -=.

Note: we need to pay attention to run addition and subtraction.

I. Writing Style of Delegate Declaration

Generally speaking, we can use the following writing.

DoSomething @do = DoA;

It is simple writing and just can be used in .NET 2.0 and last versions. In .NET 1.x, we need to write as following:

DoSomething @do = new DoSomething(DoA);

We should assign @do DoA to plus DoB when declaring.

DoSomething @do = DoA + DoB;

But this writing is useless so that we must use writing in .NET 1.x.

DoSomething @do = new DoSomething(DoA) + new DoSomething(DoB);

II. What Will Happen If Subtracting Nonexistent Method in Delegate?

Move to the following code:

DoSomething @do = DoA;
@do -= DoB;

In the first line, I declare @do and assign DoA to it. In the second line, I try to subtract DoB, which doesn't exist in method list, from @do. What will happen? There is no error given and DoA method is run correctly when calling @do. It is observed that programm can be run normally when subtracting one nonexistent method in delegate.

III. What Will Happen If all the Delegates Are Subtracted?

Move to the following code:

DoSomething @do = new DoSomething(DoA) + new DoSomething(DoB);
@do -= DoA;
@do -= DoB;
@do(1);

The code can be complied successfully, but NullReferenceException will appear when running. Therefore, we must be careful when doing subtraction for delegate.

3. Delegate Covariation

Covariation is that method parameter and return value which realize delegate can be transformed implicitly.

public delegate void DoIt(string task);

class Test
{
    static void Main(string[] args)
    {
        //DoIt Declaration. It Is Valid to Assign Parameter a Looser Method
        DoIt doIt = new DoIt(DoItImpl);
        doIt("hello");
    }

    //Looser than Delegate Defining Parameter. String Can Be Converted to object Implicitly.
    static void DoItImpl(object task)
    {
        Console.WriteLine("DoItImpl {0}",task);
    }
}

Because delegate string can be converted to DoItUmpl parameter type, object implicitly, so the above code is valid when using.

 

Original Source: http://www.cnblogs.com/yukaizhao/archive/2011/10/21/2220448.html

 

Recommendation (Component Working on Office for Silverlight and .NET):

Spire.Office: http://www.e-iceblue.com/Introduce/spire-office-for-net.html 

Spire.Doc: http://www.e-iceblue.com/Introduce/word-for-net-introduce.html

Spire.XLS: http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html

Spire.PDF: http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html

Phoenix Park

Phoenix Park, one of the biggest and the most beautiful city party of Europe, is located in Northwest suburb of Dublin. It is established in 1662 and encloses 1750 acres.

Introduction

In 1662, Phoenix Park is established as Royal Hunting Park, which includes demensne of Kilmainham Priory. After Royal Hispital at Kilmainham, started in 1680, the park was reduced to present size.

The original name of this park is Finniskk (Celtic, means clear water and green grass). Because it pronounce is similar to phoenix in English, so it is called Phoenix Park later.

At first, it is taken as enjoyment place of royal. Then, it is opened to people of Dublin by Lord Chesterfiled in 1745.

Feature Spot

Dublin Zoo

Dublin Zoo is estalished around lake in Phoenix Park. There are more than 700 animals and tropical birds living in it.

Residence of President – Áras an Uachtaráin

Áras an Uachtaráin is built in 1754. It is one white house and there is one bright light in room of the second floor. The light had never turned off from 1990 to light up the road to home for wondering irish.

Wellington Monument

Wellington Monument is established for commemorating Duke of Wellington who defeated Napoleon in Waterloo. It is almost 205 feet high, the highest monument in Europe.

Papal Cross

Papal Cross was built for the visit of Pope John Paul II on Sep. 29, 1979. The write cross is 115 feet high and built iwth steel girders.

Conclusion

As one of the Dublin’s main attractions, Phoenix Park is worth for your visitation.

Reference:

Wiki: Phoenix Park

Note: All the pictures are from internet.