protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
ArrayList arrColumn = new ArrayList();
DataSet ds = null;
ds = getDBData
arrColumn.Add(CreateBoundColumn("code_title", "구분"));
arrColumn.Add(CreateBoundColumn("dlscnt", "제정"));
arrColumn.Add(CreateBoundColumn("dlrcnt", "개정"));
arrColumn.Add(CreateBoundColumn("dltotcnt", "Total"));
arrColumn.Add(CreateBoundColumn("lscnt", "제정"));
arrColumn.Add(CreateBoundColumn("lrcnt", "개정"));
arrColumn.Add(CreateBoundColumn("ltotcnt", "Total"));
arrColumn.Add(CreateBoundColumn("scnt", "제정"));
arrColumn.Add(CreateBoundColumn("rcnt", "개정"));
arrColumn.Add(CreateBoundColumn("totcnt", "Total
string fileName = "order" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
SaveAsExcel(ds, arrColumn, "", fileName); } private 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 dgExcel = new DataGrid(); dgExcel.ShowHeader = true; dgExcel.Caption = subject; dgExcel.AutoGenerateColumns = false; foreach (object column in columns) dgExcel.Columns.Add((BoundColumn)column); dgExcel.HeaderStyle.BackColor = Color.FromName("powderblue"); dgExcel.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; dgExcel.HeaderStyle.Height = 25; dgExcel.HeaderStyle.Font.Bold = true; dgExcel.DataSource = dsResult; dgExcel.DataBind(); System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("euc-kr"); System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=euc-kr"); System.Web.HttpContext.Current.Response.ContentType = "application/unknown"; ////////////////////////////////////////////////////////// /// 한글이 깨지는 경우 web.config의 globalization을 euc-kr로 바꿔주세요. ////////////////////////////////////////////////////// System.Web.HttpContext.Current.Response.Write(""); 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(); } 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; }
by 피요히코~ 2009. 4. 17. 14:40