<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <head> <style type="text/css"> .style1 { width: 45%; } </style> </head> <form id="form1" runat="server"> <table class="style1"> <tr> <td> CodeID</td> <td> <asp:TextBox ID="txtCodeID" runat="server"></asp:TextBox> </tr> <tr> <td> Content</td> <td> <asp:TextBox ID="txtContent" runat="server"></asp:TextBox> </tr> <tr> <td> MappingCode</td> <td> <asp:TextBox ID="txtMappingCode" runat="server"></asp:TextBox> </tr> <tr> <td> </td> <td> <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /> <asp:Button runat="server" ID="Btn_Export" Text="Export" OnClick="Btn_Export_Click" /> </tr> </table> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"> <FooterStyle BackColor="#507CD1" Font-Bold ="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <EditRowStyle BackColor ="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <columns> <asp:BoundField HeaderText="CodeID" DataField="CodeID" /> <asp:BoundField HeaderText="Content" DataField="Content" /> <asp:BoundField HeaderText="MappingCode" DataField="MappingCode" /> <asp:ButtonField Text="Edit" /> <asp:ButtonField Text="Delete" /> </columns> </asp:GridView> </form>
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.Reflection; public partial class _Default : System.Web.UI.Page { private DataTable _dt; public DataTable dt { get { return _dt; } set { _dt = value; } } protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("CodeID", typeof(string)); dt.Columns.Add("Content", typeof(string)); dt.Columns.Add("MappingCode", typeof(string)); Session["dt"] = dt; } private void ExporttoExcel(DataTable table) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/ms-excel"; //HttpContext.Current.Response.ContentType = "application/ms-word"; HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls"); // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.doc"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); HttpContext.Current.Response.Write("<BR><BR><BR>"); HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>"); int columnscount = GridView1.Columns.Count; for (int j = 0; j < columnscount; j++) { HttpContext.Current.Response.Write("<Td>"); HttpContext.Current.Response.Write("<B>"); HttpContext.Current.Response.Write(GridView1.Columns[j].HeaderText.ToString()); HttpContext.Current.Response.Write("</B>"); HttpContext.Current.Response.Write("</Td>"); } HttpContext.Current.Response.Write("</TR>"); foreach (DataRow row in table.Rows) { HttpContext.Current.Response.Write("<TR>"); for (int i = 0; i < table.Columns.Count; i++) { HttpContext.Current.Response.Write("<Td>"); HttpContext.Current.Response.Write(row[i].ToString()); HttpContext.Current.Response.Write("</Td>"); } HttpContext.Current.Response.Write("</TR>"); } HttpContext.Current.Response.Write("</Table>"); HttpContext.Current.Response.Write("</font>"); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } protected void Btn_Export_Click(object sender, EventArgs e) { ExporttoExcel(dt); } protected void btnSubmit_Click(object sender, EventArgs e) { DataTable dt = (DataTable)Session["dt"]; DataRow drToGrid = dt.NewRow(); drToGrid["CodeID"] = txtCodeID.Text; drToGrid["Content"] = txtContent.Text; drToGrid["MappingCode"] = txtMappingCode.Text; dt.Rows.Add(drToGrid); GridView1.DataSource = dt; GridView1.DataBind(); } }