구 MiniHomepy/Experience

excel 파일 출력시 한글깨질때

aromacrony 2009. 5. 26. 17:09

//파일 이름 지정
string fileName = "exp"+ "5" + "_A.xls";

//컨텐츠 타입 설정
Response.ContentType = "application/vnd.ms-excel";

//컨텐츠 헤더 설정
Response.AddHeader("Content-Disposition","attachment;fileName="+fileName);

//한글로 캐릭터 셋 설정..
Response.Charset = "euc-kr";

//ViewState 사용 안함
DataGrid1.EnableViewState = false;

//쓰기 객체 생성
System.IO.StringWriter tw = new System.IO.StringWriter();

//html쓰기 객체 생성
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

//데이터 그리드의 내용을 html로 쓴다.

//내용물은 테이블 ..
이런 식이 된다.

this.ClearControls(DataGrid1);

DataGrid1.RenderControl(hw);

//한글화깨짐을 해결하는 방법..
String convStr = tw.ToString();
Response.Write(convStr);

//응답객체 종료
Response.End();