Creating an Excel Application in C#

May 31, 2020
Tutorials
excel application in c#

In this tutorial, we will be creating an excel application using C#.

This tutorial uses Microsoft.Office.Interop.Excel in our Windows Form Application.

1. Create a Windows Form Application in C# for this tutorial by following steps in Microsoft Visual Studio 2019: Go to File, click New, and choose Windows Form Application.

2. Insert a Button control named Button1 in the form. The design must be like this:

design

3. Add a reference to Microsoft Excel Object Library to your project. To do this follow the image below. Note: this is really an important library to add an excel file.

On the COM tab, locate Microsoft Excel Object Library and then click Select Microsoft Excel 14.0 Object Library. Then Click OK. Follow this image below.

On the COM tab, locate Microsoft Excel Object Library and then click Select Microsoft Excel 14.0 Object Library. Then Click OK. Follow this image below.

design

4. Put the following code in your code view:

using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
 
public class Form1
{
    private void Button1_Click(object sender, EventArgs e)
    {
        Application appXL = default(Application);
        Workbook wbXl = default(Workbook);
        Worksheet shXL = default(Worksheet);
        Range raXL = default(Range);
        // Start Excel and get Application object.
        appXL = CreateObject("Excel.Application");
        appXL.Visible = true;
        // Add a new workbook.
        wbXl = appXL.Workbooks.Add;
        shXL = wbXl.ActiveSheet;
        // Add table headers going cell by cell.
        shXL.Cells(1, 1).Value = "First Name";
        shXL.Cells(1, 2).Value = "Last Name";
        shXL.Cells(1, 3).Value = "Full Name";
        shXL.Cells(1, 4).Value = "Specialization";
        // Format A1:D1 as bold, vertical alignment = center.
        object with_1 = shXL.Range("A1", "D1");
        with_1.Font.Bold = true;
        with_1.VerticalAlignment = XlVAlign.xlVAlignCenter;
        // Create an array to set multiple values at once.
        string[,] students = new string[6, 3];
        students[0, 0] = "Lyndon";
        students[0, 1] = "Bermoy";
        students[1, 0] = "Novee";
        students[1, 1] = "Dumanig";
        students[2, 0] = "Aga";
        students[2, 1] = "Bermoy";
        students[3, 0] = "Don";
        students[3, 1] = "Bermzkiee";
        students[4, 0] = "Campcodes";
        students[4, 1] = "TheBest";
        // Fill A2:B6 with an array of values (First and Last Names).
        shXL.Range("A2", "B6").Value = students;
        // Fill C2:C6 with a relative formula (=A2 & " " & B2).
        raXL = shXL.Range("C2", "C6");
        raXL.Formula = "=A2 & \" \" & B2";
        // Fill D2:D6 values.
        shXL.Cells(2, 4).Value = "Programming";
        shXL.Cells(3, 4).Value = "Mechatronics";
        shXL.Cells(4, 4).Value = "Robotics";
        shXL.Cells(5, 4).Value = "Mathmematics";
        shXL.Cells(6, 4).Value = "Best Website";
        // AutoFit columns A:D.
        raXL = shXL.Range("A1", "D1");
        raXL.EntireColumn.AutoFit();
        // Make sure Excel is visible and give the user control
        // of Excel's lifetime.
        appXL.Visible = true;
        appXL.UserControl = true;
        // Release object references.
        raXL = null;
        shXL = null;
        wbXl = null;
        appXL.Quit();
        appXL = null;
        return;
Err_Handler:
        MsgBox(Err.Description, vbCritical, "Error: " + Err.Number);
    }
}

Output:

output

Facebook Comments

Leave a Reply

Your email address will not be published.