DataSet에 있는 내용을 엑셀로 다운로드 하기 위해서 DataGrid를 사용합니다.
DataGrid 클래스를 생성하여 DataSet을 바인딩한 다음 엑셀에 보여질 스타일등을 지정해준 다음
해당 웹페이지에 Response 하면 됩니다.
private void Page_Load(object sender, System.EventArgs e)
{
    // 엑셀로 만들 데이터를 DataSet에 채웁니다.
    DataSet dsResult = GetMemberList();  
// DataGrid와 DataTable의 각 필드에 상응하는 BoundColumn을 만들어 ArrayList에 추가합니다.
    ArrayList arrColumn = new ArrayList();
    arrColumn.Add(CreateBoundColumn("MEMID", "아이디"));
    arrColumn.Add(CreateBoundColumn("MEMNAME", "이름"));
    arrColumn.Add(CreateBoundColumn
            ("JOINDATE", "가입일", "{0:yyyy/MM/dd}", HorizontalAlign.Center));
    arrColumn.Add(CreateBoundColumn
("MILEAGE", "포인트", "{0:###,##0}", HorizontalAlign.Center));
string fileName = "members_" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
    SaveAsExcel(dsResult, arrColumn, "회원리스트", fileName);
}
public void SaveAsExcel(DataSet dsResult, ArrayList columns, string subject, string fileName)
{
    // 다운로드시 사용할 파일명을 설정합니다.
    if(fileName.Equals(null) && fileName.Equals("")) 
        fileName = DateTime.Now.ToString("yyyyMMdd")+".xls";
System.Web.HttpContext.Current.Response.Buffer = true;
// DataGrid를 생성합니다.
    DataGrid dgExcel = new DataGrid();
    dgExcel.ShowHeader = true;
dgExcel.Caption = subject;
    // DataGrid에 Header 텍스트를 추가합니다.
    dgExcel.AutoGenerateColumns = false;
    foreach(object column in columns)
        dgExcel.Columns.Add((BoundColumn)column);
// DataGrid의 스타일을 지정합니다.
    dgExcel.HeaderStyle.BackColor = Color.FromName("powderblue");
    dgExcel.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dgExcel.HeaderStyle.Height = 25;
    dgExcel.HeaderStyle.Font.Bold = true;
// DataGrid에 DataSet의 내용을 채웁니다.
    dgExcel.DataSource = dsResult;
    dgExcel.DataBind();
// DataGrid를 웹페이지에 쓰기 시작...
    System.Web.HttpContext.Current.Response.AddHeader
        ("Content-Disposition", string.Format("attachment;filename={0}", fileName));
    System.Web.HttpContext.Current.Response.ContentType = "application/unknown";
    // System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; 
    /////////////////////////////////////////////////////////////////////////////////////////////////
    ///  한글이 깨지는 경우 web.config의 globalization을 euc-kr로 바꿔주세요.
    /// <globalization requestEncoding="euc-kr" responseEncoding="euc-kr" />
    /////////////////////////////////////////////////////////////////////////////////////////////////
    System.Web.HttpContext.Current.Response.Write
        ("<meta http-equiv=Content-Type content='text/html; charset=ks_c_5601-1987'>");
dgExcel.EnableViewState = false;
    System.IO.StringWriter sWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(sWriter);
dgExcel.RenderControl(htmlWriter);
    System.Web.HttpContext.Current.Response.Write(sWriter.ToString());
    System.Web.HttpContext.Current.Response.End();
    dgExcel.Dispose();
}
// DataGrid의 반복적인 컬럼 생성 작업을 하는 메서드입니다.
public BoundColumn CreateBoundColumn(string DataFieldValue, string HeaderTextValue)
{
    // Create a BoundColumn.
    BoundColumn column = new BoundColumn();
    // Set the properties of the BoundColumn.
    column.DataField = DataFieldValue;
    column.HeaderText = HeaderTextValue;
    return column;
}
public BoundColumn CreateBoundColumn
   (string DataFieldValue, string HeaderTextValue, string FormatValue, HorizontalAlign AlignValue)
{
    // Create a BoundColumn using the overloaded CreateBoundColumn method.
    BoundColumn column = CreateBoundColumn(DataFieldValue, HeaderTextValue);
    // Set the properties of the BoundColumn.
    column.DataFormatString = FormatValue;
    column.ItemStyle.HorizontalAlign = AlignValue;
    return column;
}
'.net' 카테고리의 다른 글
| GET방식으로 한글 넘길때 인코딩 문제 (3) | 2007.05.03 | 
|---|---|
| 유저컨트롤에 포함된 컨트롤 제어하기 (0) | 2007.05.03 | 
| 잠재적 위험이 있는 Request.Form 값을 발견했습니다 (1) | 2007.05.03 | 
| ASPX페이지의 출력내용을 그대로 다운로드 하기 (2) | 2007.05.03 | 
| 현재페이지의 쿼리문자열 한꺼번에 가져오기 (0) | 2007.05.03 |