default.asox
<%@ 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">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns = "False"
Font-Names = "Arial"
Font-Size = "11pt"
AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green"
AllowPaging ="True" DataSourceID="SqlDataSource1"
>
<AlternatingRowStyle BackColor="#C2D69B"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
<asp:BoundField DataField="A" HeaderText="A" SortExpression="A" />
<asp:BoundField DataField="B" HeaderText="B" SortExpression="B" />
<asp:BoundField DataField="C" HeaderText="C" SortExpression="C" />
<asp:BoundField DataField="D" HeaderText="D" SortExpression="D" />
<asp:BoundField DataField="E" HeaderText="E" SortExpression="E" />
<asp:BoundField DataField="F" HeaderText="F" SortExpression="F" />
<asp:BoundField DataField="G" HeaderText="G" SortExpression="G" />
<asp:BoundField DataField="H" HeaderText="H" SortExpression="H" />
<asp:BoundField DataField="I" HeaderText="I" SortExpression="I" />
<asp:BoundField DataField="J" HeaderText="J" SortExpression="J" />
<asp:BoundField DataField="K" HeaderText="K" SortExpression="K" />
<asp:BoundField DataField="L" HeaderText="L" SortExpression="L" />
<asp:BoundField DataField="M" HeaderText="M" SortExpression="M" />
<asp:BoundField DataField="N" HeaderText="N" SortExpression="N" />
<asp:BoundField DataField="O" HeaderText="O" SortExpression="O" />
<asp:BoundField DataField="P" HeaderText="P" SortExpression="P" />
<asp:BoundField DataField="Q" HeaderText="Q" SortExpression="Q" />
<asp:BoundField DataField="R" HeaderText="R" SortExpression="R" />
<asp:BoundField DataField="S" HeaderText="S" SortExpression="S" />
<asp:BoundField DataField="T" HeaderText="T" SortExpression="T" />
<asp:BoundField DataField="U" HeaderText="U" SortExpression="U" />
<asp:BoundField DataField="V" HeaderText="V" SortExpression="V" />
<asp:BoundField DataField="W" HeaderText="W" SortExpression="W" />
</Columns>
<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Sheet]"></asp:SqlDataSource>
<br /><br />
<div style="width: 521px; height: 25px; float: left; background-color: #808080">
<asp:Button ID="BtnExportToWord" runat="server" Text="Export To Word"
Width="120px" onclick="BtnExportToWord_Click" />
<asp:Button ID="BtnExportToExcel" runat="server" Text="Export To Excel"
Width="120px" onclick="BtnExportToExcel_Click" />
<asp:Button ID="BtnExportToCSV" runat="server" Text="Export To CSV"
Width="120px" onclick="BtnExportToCSV_Click" />
</div>
<br /><br />
<iframe width="420" height="315" src="//www.youtube.com/embed/FPDjOngX2cw" frameborder="0" allowfullscreen></iframe>
</form>
</body>
</html>
_________________________________________________________________________________________
cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnExportToWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
protected void BtnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
protected void BtnExportToCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}