1. Using Excel COM Object
private void ExportJoinCountToExcel(Worksheet excelworksheet) { excelworksheet.Name = "가입 및 탈퇴"; int index = 2; excelworksheet.Cells[1, 1] = "날짜"; excelworksheet.Cells[1, 2] = "가입수"; excelworksheet.Cells[1, 3] = "탈퇴수"; foreach (GridViewRow row in gridviewJoinStatics.Rows) { excelworksheet.Cells[index, 1] = row.Cells[0].Text; excelworksheet.Cells[index, 2] = row.Cells[1].Text; excelworksheet.Cells[index, 3] = row.Cells[2].Text; index++; } } private void ExportPathToExcel(Worksheet excelworksheet, SyncmailPathType pathtype) { GridView gridview = null; if(pathtype == SyncmailPathType.Join) { gridview = gridviewJoinPath; excelworksheet.Name = "가입 경로"; } else { gridview = gridviewLeavePath; excelworksheet.Name = "탈퇴 경로"; } excelworksheet.Cells[1, 1] = "날짜"; List<PathCase> joinpathes = SyncmailDatabase.PathCases.GetByCase(pathtype); int index = 2; foreach (PathCase pathcase in joinpathes) { excelworksheet.Cells[1, index] = pathcase.Description; index++; } int rowindex = 2; foreach (GridViewRow row in gridview.Rows) { int columnindex = 2; excelworksheet.Cells[rowindex, 1] = row.Cells[0].Text; foreach (PathCase pathcase in joinpathes) { excelworksheet.Cells[rowindex, columnindex] = row.Cells[columnindex - 1].Text; columnindex++; } rowindex++; } }
2. Using Response.Output in ASP.NET
private void ExportToExcel(string strFileName, GridView dg) { string attachment = string.Format("attachment; filename={0}", strFileName); Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); dg.RenderControl(htw); string s = sw.ToString(); Response.Write(s); Response.End(); }