private void RetriveStock() { string constr = ConfigurationManager.ConnectionStrings["ConnectionString_master"].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("ClosingStock_WebPortal")) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@deponame", DropDownstocklocation.SelectedValue); using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = conn; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); Grid_stockdetails.DataSource = dt; Grid_stockdetails.DataBind(); //Calculate Sum and display in Footer Row decimal total = dt.AsEnumerable().Sum(row => row.Field <decimal>("Stock Value")); Grid_stockdetails.FooterRow.Cells[9].Text = "Total"; Grid_stockdetails.FooterRow.Cells[9].HorizontalAlign = HorizontalAlign.Right; Grid_stockdetails.FooterRow.Cells[10].Text = total.ToString("N2"); } } } } }
protected void exportbtn_Click(object sender, EventArgs e) { if (Grid_stockdetails.Rows.Count != 0) { Response.Clear(); Response.Buffer = true; //Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //Response.AppendHeader("content-disposition", "attachment; filename=GridViewExport.xlsx"); Response.AddHeader("content-disposition", "attachment;filename=Stock Report " + DateTime.Now.Date + ".xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); Grid_stockdetails.RenderControl(hw); //Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } }