In this article I’m going to explain how to export data from DataTable to Excel in ASP.NET using C#.

If you want to export GridView to Excel, word and PDF document please refer these articles

Export GridView data to excel or word document using ASP.NET and c#

Export GridView data to PDF document in ASP.NET using iTextSharp 

  Sometime we need to export data from DataTable to excel. Here I’ll show you how to export data to Excel by using RkLib ExportData. 

        RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch records to DataTable and we can export to excel.   

            First we have to download  RKLib ExportData is a class library. Download link is given below.

Then open your project and include the dll class library

export data to exceel

Browse RKLib class library then click ok. 

export data to exceel

Next we have to create a table like this

Table Design:

Column Name

Data Type

empid

varchar(50)

name

varchar(100)

designation

varchar(100)

city

varchar(50)

country

varchar(50)

          Here we have to create page like this.

Designer Source Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head runat="server">

    <title></title>

</head>

<body>

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

    <div>

        <asp:ImageButton ID="imgbtnExcel" runat="server"   ImageUrl="~/Images/excel_icon.png"

            OnClick="imgbtnExcel_Click" />

        <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" ShowFooter="true"

            Width="100%">

            <AlternatingRowStyle BackColor="#AED6FF" />

            <Columns>

                <asp:BoundField DataField="empid" HeaderText="Employee-ID" />

                <asp:BoundField DataField="name" HeaderText="Name" />

                <asp:BoundField DataField="designation" HeaderText="Designation" />

                <asp:BoundField DataField="city" HeaderText="City" />

                <asp:BoundField DataField="country" HeaderText="Country" />

            </Columns>

            <HeaderStyle BackColor="#0063A6" ForeColor="White" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 Note:

You have to use following Namespace 

using RKLib.ExportData;

 Code Behind:

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using RKLib.ExportData;

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

{

    SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Demo; Integrated Security=True");

    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindGrid();

        }

    } 

    protected void BindGrid()

    {      

        conn.Open();

        string cmdstr = "Select * from EmployeeDetails";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(ds);

        cmd.ExecuteNonQuery();

        gvDetails.DataSource = ds;

        gvDetails.DataBind();

        conn.Close();

    } 

    protected void imgbtnExcel_Click(object sender, ImageClickEventArgs e)

    {

        conn.Open();

        string cmdstr = "Select * from EmployeeDetails";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(ds);

        DataTable dtcaste = ds.Tables[0];

             int[] iColumns = { 0, 1, 2, 3, 4 };

            string[] sHeaders = {       

                    "Employee-ID"

                    ,"Name"

                    ,"Designation"

                    ,"City"

                    ,"Country"                   

                   }; 

            // Export the details of specified columns with specified headers to CSV

            RKLib.ExportData.Export objExport = new RKLib.ExportData.Export();

            objExport.ExportDetails(dtcaste, iColumns, sHeaders, Export.ExportFormat.CSV, "file.csv");       

    } 

}

cytotec abortion

http://www.extrageek.com/template/default.aspx?an-abortion-pill buy abortion pills online
medical abortion pill online buy cytotec for 24 week abortion abortion pill


View demo Download
http://www.dotnetfox.com/Document/1055/0Demo1.gif

If you enjoyed this article, get email updates (it's free).


Related Articles
  • Export GridView data to excel using ASP.NET and c# In this article I’m going to explain how to export GridView data to excel using ASP.NET and C#. Sometimes we need to export gridview data to excel. Here I’ll show you how to export GridView data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch the records to DataTable and we can export to excel.
  • How to export GridView data to PDF document in ASP.NET with C# using iTextSharpIn this article I’m going to explain how to export GridView to PDF document. Other articles related to iTextSharp. iTextSharp is a C# port of iText, and open source Java library for PDF generation and manipulation. It can be used to create PDF documents from scratch, to convert XML to PDF (using the extra XFA Worker DLL), to fill out interactive PDF forms, to stamp new content on existing PDF doc...
  • how to export data from DataTable to Excel in ASP.NET using C#. In this article I’m going to explain how to export data from DataTable to Excel in ASP.NET using C#. Sometime we need to export data from DataTable to excel. Here I’ll show you how to export data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch records to DataTable and we can export to excel.
  • Export Chart control to PDF document in ASP.NET using iTextSharp and C#In this article I’m going to explain how to export ASP.NET chart control to PDF using C#. I have already written an article about bind ASP.NET Chart control from database. You could refer this article by following this below link. Bind ASP.NET chart control from database using C#. If you are not aware to include iTextSharp.dll in ASP.NET project you could read this article How to create PDF docume...
  • Export HTML Table to PDF in ASP.NET In this article I’m going to explain how to export HTML table to PDF in ASP.NET. There are many ways we can export HTML Table to PDF document. Here I’ll explain the easiest way to export HTML Table to PDF. WebClient class provide the feature to download the HTML string from current URL. First we need to make sure that how we can extract HTML Table from entire source code and how we can get conten...

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox