Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Excel Export in Asp.Net MVC using XlWorkbook

Date- Jun 11,2022

9352

Free Download Pay & Download
XlWorkbook ClosedXml


XlWorkBook

XlWorkbook is a third party library that helps you to export data into excel file in easy steps. It also helps in giving formatting to excel like background color, border, color and formulas

Exporting Excel using XlWorkbook in Asp.Net MVC

So, first of all you have to install ClosedXml nuget package in your project which is showed in the image

After installing the package , you have to go to the controller and add namespace

using ClosedXML.Excel;

Now you can use Xlworkbook in your code , Xlworkbook requires data as datatable to export it into excel. Please have a look at this sample code

 public ActionResult Download()
        {
            DataTable table = DummyDataTableSource();

            using (XLWorkbook workbook = new XLWorkbook())
            {
                table.TableName = "Table 1";/*Giving table name is mandatory and it must be unique between multiple worksheets*/
                workbook.Worksheets.Add(table);
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=Dummy Excel Export.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    workbook.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                }

            }

            Response.Flush();
            Response.End();
            return View();
        }

        private static DataTable DummyDataTableSource()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name");
            table.Columns.Add("Number");
            table.Columns.Add("Address");
            table.Columns.Add("City");


            for (int i = 0; i < 10; i++)
            {
                DataRow dr = table.NewRow();
                dr["Name"] = "Name " + i;
                dr["Number"] = "Number " + i;
                dr["Address"] = "Address " + i;
                dr["City"] = "City " + i;
                table.Rows.Add(dr);
            }

            return table;
        }

Here, we have to pass the data to worksheet in the datatable format and you have to provide a tablename to datatable. We will call this action from view like this

<a style="margin-top:10px" class="btn btn-primary" href="/Home/Download">Export to Excel</a>

So, if you will click on this button you will see your data is exported in excel format. You can check in the image

So, this is how you can export data in excel using XlWorkbook in Asp.Net mvc.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Import Excel in Asp.net MVC using OLE DB
Jun 23, 2022
Integrate Stripe Payment Gateway In ASP.NET Core 8.0
Nov 23, 2023
How to Convert Text to Speech in Asp.Net
Nov 06, 2023
Integrate Stripe Payment Gateway In ASP.NET Core 7.0
Jul 22, 2023

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page