달력

5

« 2025/5 »

  • 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
  • 29
  • 30
  • 31
2014. 10. 30. 09:34

[ASP.NET] GridView 데이터 Excel로 만들기 학습/ASP.NET2014. 10. 30. 09:34



protected void SaveFileButton_Click(object sender, EventArgs e)

{

string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".xls";


Response.Clear();

Response.Buffer = true;

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

Response.Charset = "utf-8";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

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

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);


for (int i = 0; i < GridView1.Rows.Count; i++)

{

GridView1.Rows[i].Attributes.Add("class", "textmode");

}

GridView1.RenderControl(hw);


string style = @"<style> .textmode {mso-number=format:\@; } </style>";

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();

}


만약에 마스터 페이지를 사용하고있을경우 GridView1은 form태그 내부에 와야 합니다 라는 에러가뜬다.

그럴땐 VerifyRenderingInServerForm을 override시켜주면된다.


public override void VerifyRenderingInServerForm(Control control)

{

//Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time

}


VerifyRenderingInServerForm을 override시키고 Render에서 오류가 날시에는




페이지 테그단에서 EnableEventValidation값을 넣어주고 false로 설정해주면 된다.

:
Posted by 쩡용