Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Grid view to excel and save excel file to folder

Tags:

c#

asp.net

I want to save excel file which export data of grid view. I have written code to export gridview data to excel but I don't know how to save exported file.

Following is my code to export gridview into excel :

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gvFiles.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
like image 579
R.D. Avatar asked May 02 '12 10:05

R.D.


People also ask

How do I export raw data from Excel?

From your main display, select one or all chart(s) with the tag data you want to export. Navigate to the Action drop-down menu. From this drop-down menu, select Export Raw Data. The system automatically names the file and you see the download and an automatic save occurs.


3 Answers

You can do this:

private void ExportGridView()
{
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

    // Render grid view control.
    gvFiles.RenderControl(htw);

    // Write the rendered content to a file.
    string renderedGridView = sw.ToString();
    System.IO.File.WriteAllText(@"C:\Path\On\Server\ExportedFile.xlsx", renderedGridView);
}
like image 176
npclaudiu Avatar answered Sep 30 '22 04:09

npclaudiu


this may help you//

protected void exporttoexcel_Click(object sender, EventArgs e)
{
    Response.Clear();

    Response.AddHeader("content-disposition", "attachment;filename=" attachment" + ".xls");

    Response.Charset = "";

    // If you want the option to open the Excel file without saving than

    // comment out the line below

    // Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);

    GridView1.RenderControl(htmlWrite);

    Response.Write(stringWrite.ToString());

    Response.End();

}
public override void VerifyRenderingInServerForm(Control control)
{

    // Confirms that an HtmlForm control is rendered for the
    //specified ASP.NET server control at run time.

}
like image 27
writeToBhuwan Avatar answered Sep 30 '22 03:09

writeToBhuwan


public partial class exportgridtoexcel : System.Web.UI.Page
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetData();
        }
    }
    public void GetData()
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from EmpData", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        GetData();
        exporttoexcel("Report.xls", GridView1);
        GridView1 = null;
        GridView1.Dispose();



    }
    public void exporttoexcel(string filename,GridView gv)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        Response.ContentType = "applicatio/excel";
        StringWriter sw = new StringWriter(); ;
        HtmlTextWriter htm=new HtmlTextWriter(sw);
        gv.RenderControl(htm);
        Response.Write(sw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}

}

like image 34
user2089688 Avatar answered Sep 30 '22 04:09

user2089688