Hi guys,
I need scenario that while exporting GridView to excel it's not refreshing my grid data source.
Below is my code what I have done before but the GridView it's not refreshing.
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"
BackColor="Yellow" CssClass="pure-u-23-24">
</asp:DropDownList>
using (MySqlConnection con =
new MySqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
using (MySqlCommand cmd =
new MySqlCommand("sp_xls", con))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@sp_table_name",
ddlstorico.SelectedValue.ToString());
cmd.Parameters.Add(new MySqlParameter("@sp_output",
MySqlDbType.VarChar));
cmd.Parameters["@sps_output"].Direction =
ParameterDirection.Output;
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
int sps_output = Convert.ToInt32(cmd.Parameters["@sps_output"].Value);
if (sps_output > 0)
{
ExportDataSetToExcel(ds);
}
BindData();
}
}
public static void ExportDataSetToExcel(DataSet ds)
{
string AppLocation = @"D:\inetpub\wwwroot\public\ExcelFiles\";
string guid = Guid.NewGuid().ToString().ToUpper().Replace("-", "_");
HttpContext.Current.Response.Cookies["guid"].Value = guid.ToString();
HttpContext.Current.Response.Cookies["guid"].Expires = DateTime.Now.AddMinutes(3);
string filepath = AppLocation + "Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx";
using (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
wb.SaveAs(filepath);
wb.Dispose();
}
Thread.Sleep(3000);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/force-download";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx");
HttpContext.Current.Response.TransmitFile(@"D:\inetpub\wwwroot\public\ExcelFiles\Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx");
HttpCookie cookie = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
HttpContext.Current.Response.AppendCookie(cookie);
HttpContext.Current.Response.End();
}