English

Learn C# – How to create Excel (.XLS and .XLSX) file from C#?

Creating an Excel file in C# is not as hard as many beginners believe. With no internal library support for creating Excel sheets, you are left with 3rd party libraries. Thankfully, there are plenty of free 3rd party libraries that help make it easy to create .XLS and .XLSX files.

Let’s get started with the tutorial. Before we get into the code, we need to go through the library options available when creating an Excel sheet. You can use EPPlus, a C# library for creating advanced Excel spreadsheets on the server, or you can use ExcelLibrary which can get your work done without much difficulty.

For the sake of simplicity, we will be using ExcelLibrary. However, if you need more control over your Excel sheet, it is advisable to use EEPlus which is frequently updated and comes under the GNU public license(GPL)

Alright, enough theory! Let’s get to the code below.

//Table and Dataset creation

DataSet ds = new DataSet("New_DataSet");

DataTable dt = new DataTable("New_DataTable");

//Creating locale for each

ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

//Creating DB connection with OleDb.

OleDbConnection con = new OleDbConnection(dbConnectionString);

con.Open();

//Query creation.

string sql = "SELECT Whatever FROM MyDBTable;";

OleDbCommand cmd = new OleDbCommand(sql, con);

OleDbDataAdapter adptr = new OleDbDataAdapter();

adptr.SelectCommand = cmd;

adptr.Fill(dt);

con.Close();

//Adding tables

ds.Tables.Add(dt);

// DataSet used in table creation.s

ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);

Let’s go through the code to understand how it works. Most of it is self-explanatory and can be understood by the comments included within the code. The last line is what we all care about.

ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);

The library lets you create an Excel file with the help of the data set. Yup, it is that easy. For more features and functionalities, you need to visit the library page and look at its documentation.

Has anything to add to the tutorial series? Comment below and let us know.

You can also check on our website videos about C#. Below are some examples:

  • Steam/C# gamedev: customizing game server (part 2) – C#
  • C# compiler – new feature (part 33) – C#


You can also follow some of our broadcasters who program in C#, as below:

  ljw1004

 SeanColombo

Another cool way to find out interesting things about C# is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago