Hi All,
I have a GridView with 10K + data fetching from SQL Server and i have to export to excel...I have used all the code written in stackOverflow,C# cornber,ASP forums...but the problem arise when i open message popup saying the file you are trying to open is in diff. format corrupt.......something like this...i want a solution when i click export button file should be download and message should not be popup...please help i stuck from 3 days
using ClosedXML.Excel;
using System.IO;
public partial class GridExp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataSet ds = clsSQLExecute.Exec_Dataset_string("select * from UserDetails");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
gv1.DataSource = ds;
gv1.DataBind();
}
else
{
string Msg = string.Empty;
Msg = "No record Found !";
}
}
protected void btnexport_Click(object sender, EventArgs e)
{
XLWorkbook wb = new XLWorkbook();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=ExportExcel.xlsx");
MemoryStream MyMemoryStream = new MemoryStream();
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}