Creating an Excel Application in C#

Details: Creating an Excel Application

Microsoft Excel is a widely used spreadsheet application. Many people use Excel for their daily tasks. Creating an Excel application in C# is a great way to create your own spreadsheet application. Anyone can make an Excel app with C# code and then share their work with others.

In this post, we will illustrate how to create an excel application using the C# programming language.

Guide: Creating an Excel Application

1 – We start by creating a Windows Form Application in C# using Microsoft Visual Studio 2019 following the steps;
– Navigate to File
– Click on New
– Select Windows Form Application

2 – Add a Button control and give it the name Button1 in the form.
Your interface should look like the image below.

3 – In your project, add a reference to Microsoft Excel Object Library. You can find a description of how to achieve this in the image below. Please note that it is an important library to add an excel file.
– On the COM tab, find Microsoft Excel Object Library
– Select Microsoft Excel 14.0 Object Library
– Now, proceed to click OK.

4 – Insert the code below into 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] = "Durascripts";
        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);
    }
}
				
			

Outputs: Creating an Excel Application

Creating an Excel Application in C#, programming language, c#, free source code, free download, free script, free tutorial, learning

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article