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.
-
Launch
Microsoft Visual Studio
and create a new C# project. -
In the
Solution Explorer
, right-click on theReferences
option, and click onAdd Reference
from the menu bar. -
New small window will appear. From the left panel, select
COM
and then findMicrosoft Excel 16.0 Object Library
from the list and click on"OK"
.
Create the File
The following example demonstrates how to generate an Excel file through the usage of COM interop
.
-
To begin, import the following libraries.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
-
Initialize the Excel application Object before accessing the
interop
services.using Microsoft.Office.Interop.Excel; using _excel = Microsoft.Office.Interop.Excel;
-
Create an Excel application called
excelapp
, and then initialize the object ofWorkbook
andWorksheet
, which we’ll refer to aswb
andws
, respectively._Application excelapp = new _excel.Application(); Workbook wb; Worksheet ws;
-
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]; }
-
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); }
-
Create a method that will save the workbook to a path provided by the user.
public void SaveAs(string filepath) { wb.SaveAs(filepath); }
-
Inside the
Main()
method, we will initialize an object of theExcel
class calledExcelfile
.Excel Excelfile = new Excel();
-
Start a new workbook with just one sheet using the
CreateFile()
function.Excelfile.CreateFile();
-
Add a new sheet to the workbook using the
CreateSheet()
function.Excelfile.CreateSheet();
-
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:
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