- 호출하는 부분
public void ExcelDownClick(object sender, EventArgs e)
{

    Context.Items["itemname"] = "test"
    Server.Execute("ExcelDown.aspx", true);
}

- 호출되는 페이지에서 Context를 받음
string strSort = Convert.ToString(Context.Items["sort"]);


html코딩
<div id='ExcelContent' runat='server' style="display:">  //sever 컨트롤로 등록한다.
        <%=strExcelContent %>
    </div>

- div에 동적으로 표시
private void ExcelTabel(DataTable ExcelTable, string[] strExcelHeader, int nColspan, string strTitle)
        {
            StringBuilder sb = new StringBuilder();
            string strBackColor = Skcc.Configuration.SkccFxConfigManager.GetString("BackColor");
               
            sb.Append("<table>");
            sb.Append("<tr>");
            sb.Append("<th colspan=" + strExcelHeader.Length.ToString() + ">");
            sb.Append("<h2>" + strTitle + "</h2>");
            sb.Append("</th>");
            sb.Append("</tr><tr>");
            sb.Append("</tr>");
            sb.Append("</table>");

            sb.Append("<table border='1' style='border-bottom-width:1px; border-left-width:1px; border-right-width:1px; border-top-width:1px;'>");
            sb.Append("<tr>");
            for (int h = 0; h < strExcelHeader.Length; h++)
            {
                sb.Append("<th style='background-color:").Append(strBackColor).Append(";'  >");
                sb.Append(strExcelHeader[h]);
                sb.Append("</th>");
            }
            sb.Append("</tr>");
            for (int i = 0; i < ExcelTable.Rows.Count; i++)
            {
                sb.Append("<tr>");
                string strCount = string.Empty;
                for (int j = 0; j < nColspan; j++)
                {
                    if (ExcelTable.Columns[j].ColumnName.Equals("Country"))
                    {
                        string strValue = ExcelTable.Rows[i][j].ToString();
                        if (ExcelTable.Rows[i][j].ToString().Equals("Etc"))
                        {
                            strValue = Resources.GIFT.CM_Unknown;
                        }
                        sb.Append(" <td >");
                        sb.Append(strValue.Replace("\r\n", "<br/>"));
                        sb.Append(" </td>");
                    }else
                    {
                        sb.Append(" <td >");
                        sb.Append(ExcelTable.Rows[i][j].ToString().Replace("\r\n", "<br/>"));
                        sb.Append(" </td>");  
                    }
                }
                sb.Append("</tr>");

            }
            sb.Append("</table>");
           
            strExcelContent += sb.ToString(); 
        }


//엑셀로 변환함
private void ExcelDownLoad(string strTitle)
        {
            Response.Clear();
            string strFileName = HttpUtility.UrlEncode(strTitle);
            strFileName = strFileName.Replace("+", "%20");
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", strFileName));
            Response.ContentType = "application/vnd.ms-excel";

            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=''text/html; charset=utf-8''>");
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("euc-kr");
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

            ExcelContent.RenderControl(htmlWriter);  //div 컨트롤에 Rendering함

            Response.Write(stringWriter.ToString());
            Response.End();
        }



'asp.net' 카테고리의 다른 글

asp.net 이벤트 호출 순서  (2) 2010.09.09
이미지 Thumbnail(크기조정)  (0) 2010.09.09
updatepanel 안에 Repeater사용  (2) 2010.09.09
javascript에서 dopostback 호출하기  (2) 2010.09.09
updatePanel 사용  (0) 2010.09.09
Posted by 정광원
,