تبدیل محتوای کنترل GridView به فایل متنی
چهارشنبه 9 اردیبهشت 1394در این مقاله نحوه تبدیل محتوای GridView به فایل متنی را شرح خواهیم داد
در ابتدا باید بانک اطلاعاتی خود را ایجاد کنیم , برای این کار وارد محیط Sql می شویم و جدولی(Employee) با مشخصات زیر ایجاد میکنیم ,
سپس چند رکورد به صورت پیش فرض در داخل جدول وارد می کنیم .
سپس یک کنترل Gridview و یک دکمه برای تبدیل محتوای GridView به فایل متنی , در صفحه قرار میدهیم , مشابه تگ زیر :
<%@ 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>EXPORT Grid View To a Text File in ASP.NET C#</title> </head> <body> <form id="form1" runat="server"> <div> <table style="border: solid 15px blue; width: 100%; vertical-align: central;"> <tr> <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-size: 20pt; color: orangered;"> EXPORT Grid View To a Text File in ASP.NET C# </td> </tr> <tr> <td style="text-align: left; padding-left: 50px; border: solid 1px red;"> <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="90%" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both"> <Columns> <asp:BoundField DataField="Name" HeaderText="Employee Name" /> <asp:BoundField DataField="Designation" HeaderText="Designation" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:BoundField DataField="State" HeaderText="State" /> <asp:BoundField DataField="Country" HeaderText="Country" /> </Columns> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> <RowStyle BackColor="White" ForeColor="#003399" /> <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> </asp:GridView> </td> </tr> <tr> <td align="right"> <asp:Button ID="btnExport" Text="Export Grid View" OnClick="btnExport_Click" runat="server" /> </td> </tr> </table> </div> </form> </body> </html>
سپس برای اتصال کنترل GridView به بانک اطلاعاتی از دستورات زیر استفاده میکنیم :
private void BindGrid() { SqlConnection con = new SqlConnection(); ds = new DataSet(); con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"; SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con); da = new SqlDataAdapter(cmd); da.Fill(ds); con.Open(); cmd.ExecuteNonQuery(); con.Close(); if (ds.Tables[0].Rows.Count > 0) { GridViewEmployee.DataSource = ds.Tables[0]; GridViewEmployee.DataBind(); } }
در قسمت رویداد کلیک دکمه , کد زیر را برای تبدیل به فایل متنی می نویسیم:
protected void btnExport_Click(object sender, EventArgs e) { string txtFile = string.Empty; //Adding Column Name In Text File. foreach (TableCell cell in GridViewEmployee.HeaderRow.Cells) { txtFile += cell.Text + "\t\t"; } txtFile += "\r\n"; //Adding Data Column Values in Text File foreach (GridViewRow row in GridViewEmployee.Rows) { foreach (TableCell cell in row.Cells) { txtFile += cell.Text + "\t\t"; } txtFile += "\r\n"; } Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=EmployeeData.txtFile"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(txtFile); Response.Flush(); Response.End();
در ادامه کلیه دستورات صفحه را به صورت کامل میتوانید مشاهده کنید :
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.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlDataAdapter da; DataSet ds = new DataSet(); DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) this.BindGrid(); } private void BindGrid() { SqlConnection con = new SqlConnection(); ds = new DataSet(); con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"; SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con); da = new SqlDataAdapter(cmd); da.Fill(ds); con.Open(); cmd.ExecuteNonQuery(); con.Close(); if (ds.Tables[0].Rows.Count > 0) { GridViewEmployee.DataSource = ds.Tables[0]; GridViewEmployee.DataBind(); } } protected void btnExport_Click(object sender, EventArgs e) { string txtFile = string.Empty; //Adding Column Name In Text File. foreach (TableCell cell in GridViewEmployee.HeaderRow.Cells) { txtFile += cell.Text + "\t\t"; } txtFile += "\r\n"; //Adding Data Column Values in Text File foreach (GridViewRow row in GridViewEmployee.Rows) { foreach (TableCell cell in row.Cells) { txtFile += cell.Text + "\t\t"; } txtFile += "\r\n"; } Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=EmployeeData.txtFile"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(txtFile); Response.Flush(); Response.End(); } }
- ASP.net
- 1k بازدید
- 0 تشکر