티스토리 뷰
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class GridViewExportUtil
{
public static void Export(string fileName, GridView gv)using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class GridViewExportUtil
{
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}
Now export to excel in button click, here is the code
protected void Button1_Click(object sender, EventArgs e)
{
GridViewExportUtil.Export("Grid.xls", this.Gridview1);
}
{
GridViewExportUtil.Export("Grid.xls", this.Gridview1);
}
'Study > Language >' 카테고리의 다른 글
알아두면 유용한 VI편집기 명령어 환경설정 (0) | 2009.12.24 |
---|---|
How to locate index of ListBox control at last index in C# (0) | 2009.10.22 |
Dev C++ 인스톨파일 (0) | 2009.10.18 |
OnDropFiles() (0) | 2009.09.16 |
MFC 다이얼로그기반 종료방지코드 (0) | 2009.09.14 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그래비트랙스
- jakeyoon
- 소설네트워크2
- 안드로이드폰
- 리눅스2.6
- 안드로이드
- 릴레이대회본선
- 리눅스 커널 2.6 구조와 원리
- 서버개발자
- Developing on AWS
- watchface
- GDG
- yjaeseok
- jaeseokyoon
- Linux
- 창의과학교구
- gdgssu
- gdg watchfacehack
- 리눅스
- WatchFaceHack
- 숭실대
- 윤재석
- 창조경제혁신센터
- 해커톤
- 하쭈서쭈
- Jake Yoon
- 리눅스 커널 2.6
- AndroidWear
- GDG SSU
- 핸즈온머신러닝
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함