HOW TO CREATE EXCEL FILE IN ASP.NET C# - USING CLOSEDXML LIBRARY

in #programming6 years ago

HOW TO CREATE EXCEL FILE IN ASP.NET C# - USING CLOSEDXML LIBRARY

HERE IN THIS .Net Tutorial WE WILL CREATE EXCEL FILE IN ASP.NET C# - USING CLOSEDXML LIBRARY

Step 1 :-Create WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GenerateExcelFile.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create Excel File" />

<br />

<br />

<asp:Label ID="Label1" runat="server" ForeColor="Lime"></asp:Label>

        </div>

    </form>

</body>

</html>


Step 2:-Create WebForm1


using ClosedXML.Excel;

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


namespace GenerateExcelFile

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{


}


protected void Button1_Click(object sender, EventArgs e)

{

string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))

{

using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))

{

using (SqlDataAdapter sda = new SqlDataAdapter())

{

cmd.Connection = con;

sda.SelectCommand = cmd;

using (DataTable dt = new DataTable())

{

sda.Fill(dt);

using (XLWorkbook wb = new XLWorkbook())

{

wb.Worksheets.Add(dt, "Customers");

Response.Clear();

Response.Buffer = true;

Response.Charset = "";

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Response.AddHeader("content-disposition", "attachment;filename=Customer.xlsx");

using (MemoryStream MyMemoryStream = new MemoryStream())

{

wb.SaveAs(MyMemoryStream);

MyMemoryStream.WriteTo(Response.OutputStream);

Response.Flush();

Response.End();

}

}

}

}

}

}

Label1.Text = "Sucessfully Done.";

}

}

}


If You Haven't Subscribers My Channel Than Please Subscribers It And Even Share It Link Of My Channnel :- https://www.youtube.com/channel/UCdIDIGUh3rL9A_Yt_tbZmkg Please Go And Subscribers It

Sort:  

If You Haven't Subscribers My Channel Than Please Subscribers It And Even Share It Link Of My Channnel :- https://www.youtube.com/channel/UCdIDIGUh3rL9A_Yt_tbZmkg Please Go And Subscribers It

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63004.58
ETH 2548.62
USDT 1.00
SBD 2.81