ASP.NET

ASP.NET ile GridView’ deki Verileri Word ve Excel Dosyasına Aktarma – Web Tasarım & Programlama

 

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;

using System.Configuration;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if(!IsPostBack)

        {

            string cs = “Server=.;Initial Catalog=kutuphane;Integrated Security=SSPI”;

            using (SqlConnection con = new SqlConnection(cs))

            {

                SqlDataAdapter da = new SqlDataAdapter(“SELECT *FROM ogrenci WHERE ogrno<11”,con);

                DataSet ds = new DataSet();

                da.Fill(ds);

                GridView1.DataSource = ds;

                GridView1.DataBind();

 

            }

        }

    }

 

    protected void Button1_Click(object sender, EventArgs e)//Excel

    {

        Response.ClearContent();

        Response.AppendHeader(“content-disposition”, “attachement;filename=ogrenciListe.xls”);

        Response.ContentType = “application/excel”;

        StringWriter sw = new StringWriter();

        HtmlTextWriter ht = new HtmlTextWriter(sw);

        GridView1.HeaderRow.Style.Add(“background-color”, “#FFFFFF”);

        foreach (TableCell tableCell in GridView1.HeaderRow.Cells)

        {

            tableCell.Style[“background-color”] = “#A55129”;

        }

        foreach (GridViewRow gridViewRow in GridView1.Rows)

        {

            gridViewRow.BackColor = System.Drawing.Color.White;

            foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)

            {

                gridViewRowTableCell.Style[“background-color”] = “#FFF7E7”;

            }

        }

 

        GridView1.RenderControl(ht);

        Response.Write(sw.ToString());

        Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)

    {

 

    }

 

 

    protected void Button2_Click(object sender, EventArgs e)

    { //Word Aktarımı

        Response.ClearContent();

        Response.AppendHeader(“content-disposition”, “attachement;filename=ogrenciListe.doc”);

        Response.ContentType = “application/word”;

        StringWriter sw = new StringWriter();

        HtmlTextWriter ht = new HtmlTextWriter(sw);

        GridView1.HeaderRow.Style.Add(“background-color”, “#FFFFFF”);

        foreach (TableCell tableCell in GridView1.HeaderRow.Cells)

        {

            tableCell.Style[“background-color”] = “#A55129”;

        }

        foreach (GridViewRow gridViewRow in GridView1.Rows)

        {

            gridViewRow.BackColor = System.Drawing.Color.White;

            foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)

            {

                gridViewRowTableCell.Style[“background-color”] = “#FFF7E7”;

            }

        }

 

        GridView1.RenderControl(ht);

        Response.Write(sw.ToString());

        Response.End();

    }

}

 

İlgili Makaleler

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Başa dön tuşu