Here we
will learn How to export grid data into Excel
And Word .
Add an aspx
page and write the following code on .aspx Page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Export Gridview with Images
in Asp.net</title>
<style type="text/css">
.GridviewDesign {font-size: 100%; font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif color: #303933;}
.headerstyleForGrid
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDesign">
<asp:GridView ID="gvDetailsForExport" CssClass="Gridview" runat="server" AutoGenerateColumns="False">
<HeaderStyle CssClass="headerstyleForGrid" />
<Columns>
<asp:BoundField HeaderText="User Id" DataField="UserId" />
<asp:BoundField HeaderText="User Name" DataField="UserName" />
<asp:BoundField HeaderText="Gender" DataField="Gender" />
<asp:ImageField DataImageUrlField="Imagepath" HeaderText="Image" ItemStyle-Height="25px" ItemStyle-Width="25px" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportToExcel" runat="server" Text="Export Data To
Excel"
onclick="btnExport_Click" />
<asp:Button ID="btnExportToWord" runat="server" Text="Export Data To
Word"
onclick="btnExportToWord_Click" />
</div>
</form>
</body>
</html>
|
After
completion code for .aspx page write the following code on .aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvDetailsForExport.DataSource
= BindData();
gvDetailsForExport.DataBind();
}
}
protected DataTable BindData()
{
DataTable dtGrid = new DataTable();
dtGrid.Columns.Add("UserId", typeof(Int32));
dtGrid.Columns.Add("UserName", typeof(string));
dtGrid.Columns.Add("Gender", typeof(string));
dtGrid.Columns.Add("Imagepath", typeof(string));
dtGrid.Rows.Add(1,"Munesh","Male","Images/uploads/Sign1.jpg");
dtGrid.Rows.Add(2,"Rahul","Male","Images/uploads/Sign2.jpg");
dtGrid.Rows.Add(3,"Reet","Female","Images/uploads/Sign3.jpg");
dtGrid.Rows.Add(4,"Anshuman","Male","Images/uploads/Sign4.jpg");
return dtGrid;
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "Employees.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvDetailsForExport.AllowPaging = false;
gvDetailsForExport.DataSource =
BindData();
gvDetailsForExport.DataBind();
gvDetailsForExport.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i <
gvDetailsForExport.HeaderRow.Cells.Count; i++)
{
gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvDetailsForExport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void btnExportToWord_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "Employees.doc"));
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvDetailsForExport.AllowPaging = false;
gvDetailsForExport.DataSource =
BindData();
gvDetailsForExport.DataBind();
//Change the Header Row back to white color
gvDetailsForExport.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i <
gvDetailsForExport.HeaderRow.Cells.Count; i++)
{
gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvDetailsForExport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
|
Now run your application and see the output and
perform operation On Gridview.
Click Here to Download Semple code Download