asp.net DataGrid를 간단하게 Excel로 변환하기

asp.net DataGrid를 간단하게 Excel로 변환하기

public class GridExportTest : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid DataGrid1;

protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)

{

SqlConnection con = new SqlConnection();

con.ConnectionString = "server=.;database=pubs;uid=***;pwd=***";

SqlCommand cmd = new SqlCommand("Select * from Authors", con);

SqlDataAdapter a = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

a.Fill(ds);

DataGrid1.DataSource = ds;

DataGrid1.DataBind();

}

private void Button1_Click(object sender, System.EventArgs e)

{

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=DataGrid.xls");

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

DataGrid1.RenderControl(htmlWriter);

Response.Write(stringWrite.ToString());

Response.End();

}

}

from http://pengs.tistory.com/44 by ccl(A)

댓글

이 블로그의 인기 게시물

🐱‍💻Installing IIS Components Windows 10

2017년 1월 스타트업에서 구인할때 주로 원하는 개발 기술

Creating Cascading DropDownLists in ASP.Net (연속적인 셀렉트박스...