Thursday 18 September 2014

How to Export GridView Data to Excel and Word in Asp.Net

How to Export GridView Data to Excel and Word in Asp.Net……………………….

Note :In this topic we will discuss how to export GridView Data to Excel and Word in Asp.Net

--sql query………………………………………….

create database GridConcept
use GridConcept
create table Employee
(
SrNo int identity(1,1) primary key,
Name varchar(50),
Gender varchar(50),
Address varchar(50),
MobileNo varchar(50)
)

Default.aspx ………………………………………

<%@ 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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<font color="blue"><b><center>How to Export GridView Data to Excel and Word in Asp.Net</center></b></font> </td>
</tr>
<tr>
<td >
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowdatabound="GridView1_RowDataBound">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnexportExcel" runat="server"
Text="Export GridView to Excel" onclick="btnexportExcel_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
<asp:Button ID="btnExportWord" runat="server"
Text="Export GridView to Word" onclick="btnExportWord_Click" />
</td></tr>
</table>
</div>
</form>
</body>
</html>


Default.aspx.cs ………………………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
//using this namespace……………………………
using System.IO;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// CODE FOR BIND GRIDVIEW ON PAGELOAD ................
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=KUSHTIWARI-PC;Initial Catalog=GridConcept;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Employee", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

}

//code for rowdatabound event on gridview......................
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String mno = DataBinder.Eval(e.Row.DataItem, "MobileNo").ToString();
if (mno.Length < 10)
{
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].Font.Bold = true;
e.Row.Cells[4].Font.Size = 12;
}
}
}

// Export GridView to Excel................

protected void btnexportExcel_Click(object sender, EventArgs e)
{

Response.ClearContent();
Response.ContentType = "application/excel";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

}

// this code for removing this type error  on page  Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

public override void VerifyRenderingInServerForm(Control control)
{

}

// Export GridView to Word..................

protected void btnExportWord_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.ContentType = "application/word";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.doc");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.HeaderRow.Style.Add("background-color", "#990000");
foreach (TableCell tc  in GridView1.HeaderRow.Cells)
{
tc.Style["background-color"] = "#990000";
}

foreach (GridViewRow grv in GridView1.Rows)
{
// <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
GridView1.BackColor = Color.White;
foreach (TableCell gridRowTableCell in grv.Cells)
{
gridRowTableCell.Style["background-color"] = "#FFCC66";
}
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}



0 comments:

Post a Comment