Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories



I am trying to export data in a Gridview to Excel and store that file in a folder on server.
I have done this part. The only thing I want to do is,
I want to prevent downloading the Excel file.
Please find my code below.

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "order.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gridX.AllowPaging = false;
    bindX();
    gridX.HeaderRow.Style.Add("background-color", "#FFFFFF");
    for (int i = 0; i < gridX.HeaderRow.Cells.Count; i++)
    {
        gridX.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
    }
    gridX.RenderControl(htw);
    //Response.Write(sw.ToString());
    string renderedGridView = sw.ToString();
    string path = Server.MapPath("~/Order/od/x");
    System.IO.File.WriteAllText(path + "/order" + lblF.Text + ".xls", renderedGridView);
    sw.Close();
    htw.Close();


Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use this code by editing as per your need :

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    e.Cancel = true;
    WebClient client = new WebClient();

    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);

    client.DownloadDataAsync(e.Url);
}

void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    string filepath = textBox1.Text;
    File.WriteAllBytes(filepath, e.Result);
    MessageBox.Show("File downloaded");
}

Hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...