제목 : Re : gridview 내용을 엑셀에 보내는데요,애러가 떠요
글번호:
|
|
584
|
작성자:
|
|
김동규
|
작성일:
|
|
2007/05/09 오전 1:19:00
|
조회수:
|
|
3607
|
역시나 VB가 아닌 C#입니다.
엑셀과 워드 둘 다 됩니다.
public partial class GridViewToExcel_Word : System.Web.UI.Page
{
protected DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
ReadData();
}
protected void ReadData()
{
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString = "Server=localhost;uid=sa;password=sa;database=Northwind";
SqlDataAdapter Adapt = new SqlDataAdapter("Select * from Employees", objCon);
//DataSet ds = new DataSet();
ds = new DataSet();
Adapt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
//엑셀로 파일...
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;fileName=" + "GridViewToExcel.xls");
Response.Charset = "";
//Response.Charset = "euc-kr";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView1.EnableViewState = false;
//ds를 전역으로 선언
GridView dv = new GridView(); //새로운 gridview를 생성
dv.DataSource = ds;
dv.DataBind();
dv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void btnWord_Click(object sender, EventArgs e)
{
Response.Clear();
// 워드파일로
Response.AddHeader("content-disposition", "attachment;filename=" + "GridViewToWord.doc");
Response.ContentType = "application/vnd.word";
Response.Charset = "";
//Response.Charset = "euc-kr";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView1.EnableViewState = false;
//ds를 전역으로 선언
GridView dv = new GridView(); //새로운 gridview를 생성
dv.DataSource = objDs;
dv.DataBind();
dv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
}
On 2007-05-08 오후 7:47:00, '하나' wrote:
------------------------------------------------------------
> Dim ds_emp As DataTable = bc.GetBCTHZAuthGrp(ddl_id.Text, ddl_yn.Text)
>
> Response.Clear()
> Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
> Response.Charset = "gb2312"
> Response.ContentType = "application/vnd.xls"
>
> Dim stringWrite As IO.StringWriter = New System.IO.StringWriter()
>
> Dim htmlWrite As Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
>
> GV_authGrp.AllowPaging = False
> GV_authGrp.DataSource = ds_emp
> GV_authGrp.DataBind()
> ----> 여기서 애러나요 GV_authGrp.RenderControl(htmlWrite)
> Response.Write(stringWrite.ToString())
> Response.End()
>
>감사합니다
------------------------------------------------------------