How to Create Excel File in C#

Muhammad Zeeshan Feb 02, 2024
How to Create Excel File in C#

Using the C# programming language, we’ll learn how to generate an Excel Spreadsheet.

Create Excel File in C#

Before moving on, check whether your device has the Microsoft Excel 16.0 Object Library component of the .NET Framework, which confirms a successful installation. If it has, you may proceed.

We will use Microsoft Visual Studio for this project.

Add Refference

To create an Excel file, we need to add the Microsoft Excel 16.0 Object Library as a reference. Follow these steps below if you want to know how to do so.

  1. Launch Microsoft Visual Studio and create a new C# project.

  2. In the Solution Explorer, right-click on the References option, and click on Add Reference from the menu bar.

    Microsoft Visual Studio - Add Reference

  3. New small window will appear. From the left panel, select COM and then find Microsoft Excel 16.0 Object Library from the list and click on "OK".

    Microsoft Visual Studio - Reference Manager

Create the File

The following example demonstrates how to generate an Excel file through the usage of COM interop.

  1. To begin, import the following libraries.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
  2. Initialize the Excel application Object before accessing the interop services.

    using Microsoft.Office.Interop.Excel;
    using _excel = Microsoft.Office.Interop.Excel;
    
  3. Create an Excel application called excelapp, and then initialize the object of Workbook and Worksheet, which we’ll refer to as wb and ws, respectively.

    _Application excelapp = new _excel.Application();
    Workbook wb;
    Worksheet ws;
    
  1. Create a method called CreateFile(). When the method is executed, it will produce a new Excel document by producing a new Excel workbook containing just one worksheet.

    public void CreateFile() {
      this.wb = excelapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
      this.ws = this.wb.Worksheets[1];
    }
    
  2. Similarly, create a method called CreateSheet() that will add a blank worksheet to the current workbook that has been opened.

    public void CreateSheet() {
      Worksheet worksheet1 = excelapp.Worksheets.Add(After: this.ws);
    }
    
  3. Create a method that will save the workbook to a path provided by the user.

    public void SaveAs(string filepath) {
      wb.SaveAs(filepath);
    }
    
  4. Inside the Main() method, we will initialize an object of the Excel class called Excelfile.

    Excel Excelfile = new Excel();
    
  5. Start a new workbook with just one sheet using the CreateFile() function.

    Excelfile.CreateFile();
    
  6. Add a new sheet to the workbook using the CreateSheet() function.

    Excelfile.CreateSheet();
    
  7. Save the file in a specified path using the SaveAs() function.

    Excelfile.SaveAs(@"D:myarticle/ExcelfilebyZeeshan.xlsx");
    Excelfile.wb.Close();
    

Complete Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using _excel = Microsoft.Office.Interop.Excel;

namespace CreateExcelcsharpZeeshan {
  class Excel {
    _Application excelapp = new _excel.Application();
    Workbook wb;
    Worksheet ws;

    public void CreateFile() {
      this.wb = excelapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
      this.ws = this.wb.Worksheets[1];
    }

    public void CreateSheet() {
      Worksheet worksheet1 = excelapp.Worksheets.Add(After: this.ws);
    }

    public void SaveAs(string filepath) {
      wb.SaveAs(filepath);
    }

    public static void Main(string[] args) {
      Excel Excelfile = new Excel();
      Excelfile.CreateFile();
      Excelfile.CreateSheet();
      Excelfile.SaveAs(@"D:myarticle/ExcelfilebyZeeshan.xlsx");
      Excelfile.wb.Close();
    }
  }
}

Output:

successfully created an excel file

Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Csharp Excel