본문 바로가기
[ Program ]/C#

GridView에서 엑셀로 저장하기

by 관이119 2012. 9. 18.

출처 용의 꼬리보다 뱀의 머리가 낫다 .. | 사고뭉치
원문 http://blog.naver.com/mininuke7303/50021676904

 

ArrayList arr;

protected void btnSaveToExcel_Click(object sender, EventArgs e)
{
// Disable paging
gvTranslationRequestList.AllowPaging = false;
//LoadGridData();
gvTranslationRequestList.DataBind();
// exluded columns arraylist
ArrayList defaultExcludedColumns = new ArrayList();
// Always exclude these columns
defaultExcludedColumns.Add("MyHiddenFieldName");
// Send to base Excel export method
ExportGridView(gvTranslationRequestList, "TranslationRequest", defaultExcludedColumns);
// Rebind with paging enabled
gvTranslationRequestList.AllowPaging = true;
//LoadGridData();
gvTranslationRequestList.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
}
/// <summary>
/// Export GridView data to Excel.
/// </summary>
/// <param name="grdView">GridView control to export.</param>
/// <param name="filename">Filename of excel spreadsheet.</param>
/// <param name="excludedColumnList">ArrayList of columns to exlude.</param>
protected void ExportGridView(GridView grdView, string filename, ArrayList excludedColumnList)
{
// Clear response content & headers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
// Add header
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.ContentType = "application/vnd.xls";
// Create stringWriter
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
// Create HtmlTextWriter
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
// Remove controls from Column Headers
if (grdView.HeaderRow != null && grdView.HeaderRow.Cells != null)
{
for (int ct = 0; ct < grdView.HeaderRow.Cells.Count; ct++)
{
// Save initial text if found
string headerText = grdView.HeaderRow.Cells[ct].Text;
// Check for controls in header
if (grdView.HeaderRow.Cells[ct].HasControls())
{
// Check for link button
if (grdView.HeaderRow.Cells[ct].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
// link button found, get text
headerText = ((LinkButton)grdView.HeaderRow.Cells[ct].Controls[0]).Text;
}
// Remove controls from header
grdView.HeaderRow.Cells[ct].Controls.Clear();
}
// Reassign header text
grdView.HeaderRow.Cells[ct].Text = headerText;
}
}
// Remove footer
if (grdView.FooterRow != null)
{
grdView.FooterRow.Visible = false;
}
// Remove unwanted columns (header text listed in removeColumnList arraylist)
foreach (DataControlField field in grdView.Columns)
{
if (excludedColumnList.Contains(field.HeaderText))
{
field.Visible = false;
}
}
// Call gridview's renderControl
grdView.RenderControl(htmlWrite);
// Write Response to browser
Response.Write(stringWrite.ToString());
Response.End();
}

'[ Program ] > C#' 카테고리의 다른 글

[C#]랜덤파일명 생성하는 방법  (0) 2012.09.18
HELLO WORLD로 보는 C#의 세계  (0) 2012.09.18
C#과 API  (0) 2012.09.18
.NET Framework의 강력한 이름 및 보안  (0) 2012.09.18
StringBuilder 클래스  (0) 2012.09.18

댓글