Monday, 5 August 2013
Export Grid into Excel (Simple way)
Design:
Database:
Design code:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="export to Excel" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dreamthewebConnectionString %>"
SelectCommand="SELECT [name], [email], [age], [phone] FROM [account]"></asp:SqlDataSource>
</form>
.cs page:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Charset = "";
Response.AddHeader("content-disposition","attachment;filename=account.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Buffer = true;
StringWriter strinwrter = new StringWriter();
HtmlTextWriter htmlwrter = new HtmlTextWriter(strinwrter);
Table table1 = new Table();
TableRow tablerow1 = new TableRow();
TableCell tablecell1 = new TableCell();
tablecell1.Controls.Add(GridView1);
tablerow1.Cells.Add(tablecell1);
table1.Rows.Add(tablerow1);
table1.RenderControl(htmlwrter);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(strinwrter.ToString());
Response.Flush();
Response.End();
}
Database:
Design code:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="export to Excel" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dreamthewebConnectionString %>"
SelectCommand="SELECT [name], [email], [age], [phone] FROM [account]"></asp:SqlDataSource>
</form>
.cs page:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Charset = "";
Response.AddHeader("content-disposition","attachment;filename=account.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Buffer = true;
StringWriter strinwrter = new StringWriter();
HtmlTextWriter htmlwrter = new HtmlTextWriter(strinwrter);
Table table1 = new Table();
TableRow tablerow1 = new TableRow();
TableCell tablecell1 = new TableCell();
tablecell1.Controls.Add(GridView1);
tablerow1.Cells.Add(tablecell1);
table1.Rows.Add(tablerow1);
table1.RenderControl(htmlwrter);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(strinwrter.ToString());
Response.Flush();
Response.End();
}
0 comments:
Post a Comment