public static void MultitbleDataset2SingleSheetGridExport(GridView[] gv, String filename) { string Filename = filename + ".xls"; //if (ds == null || ds.Tables.Count == 0) //{ // throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset"); //} HttpResponse Response = System.Web.HttpContext.Current.Response; Response.Clear(); Response.Buffer = true; Response.ClearContent(); Response.AddHeader("content-disposition", "attachment;filename=" + Filename); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); Table tb = new Table(); for (int i = 0; i < gv.Length; i++) { GridView gridvw = new GridView(); // string grid = ConvertDataTableToHTML(ds.Tables[i]); //gridvw.DataSource = gv[i]; //gridvw.DataBind(); PrepareForExport(gv[i]); TableRow tr1 = new TableRow(); TableCell cell1 = new TableCell(); cell1.Controls.Add(gv[i]); tr1.Cells.Add(cell1); TableCell cell2 = new TableCell(); cell2.Text = " "; TableRow tr2 = new TableRow(); tr2.Cells.Add(cell2); TableRow tr3 = new TableRow(); tb.Rows.Add(tr1); tb.Rows.Add(tr2); tb.Rows.Add(tr3); } tb.RenderControl(htw); //style to format numbers to string string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } public static void PrepareForExport(GridView Gridview) { Gridview.DataBind(); //Change the Header Row back to white color Gridview.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells for (int k = 0; k < Gridview.HeaderRow.Cells.Count; k++) { Gridview.HeaderRow.Cells[k].Style.Add("background-color", "#436F9F"); } for (int i = 0; i < Gridview.Rows.Count; i++) { GridViewRow row = Gridview.Rows[i]; //Change Color back to white row.BackColor = System.Drawing.Color.White; row.Attributes.Add("class", "textmode"); row.Attributes.Remove("image"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { for (int j = 0; j < Gridview.Rows[i].Cells.Count; j++) { row.Cells[j].Style.Add("background-color", "#C2D69B"); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)