简体   繁体   中英

How can i save excel file in downloads folder using asp .net c#

Below is my code to download the data in excel but the problem is while downloading it is not showing that the file is getting downloaded moreover i am giving the path in this way as given below to download the file in downloads folder but i should not use this because it works in local host but it will not work when hosted in server.how can i download into downloads folder with showing the downloading file at bottom

 protected void btnExportExcel_Click(object sender, EventArgs e)
    {
    string pathDownload = @"~\Downloads\" Data.xls";
    ExportToExcel(dsExcel, pathDownload);
    lblMessage.Text = "Downloaded Successfully";
    }
    private void ExportToExcel(DataSet table, string filePath)
    {

   int tablecount = table.Tables.Count;
        StreamWriter sw = new StreamWriter(filePath, false);
        sw.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
        sw.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
sw.Write("<BR><BR><BR>");
            sw.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:'#1E90FF'> <TR>");
 sw.Write("</Table>");
            //sw.Write("<BR><BR><BR><BR>");
            //sw.Write("\n");
            //sw.Write(string.Format("Line1{0}Line2{0}", Environment.NewLine));


            sw.Write("</font>");

        }
        sw.Close();
    }
    this is the path that i am getting ~\Downloads\DATA.xls

    and i am getting this exception Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\~\Downloads\DATA.xls'. StreamWriter sw = new StreamWriter(filePath, false);

Currently your streamwriter writes to a local file. You need to get it to write to the browser. So instead of

StreamWriter sw = new StreamWriter(filePath, false);

Use

StreamWriter sw = new StreamWriter(HttpContext.Current.Response.OutputStream);

Also, be sure to set the right MIME type and set the content-disposition to trigger a download .

private void ExportGridToExcel() {

        Maingrid.AllowPaging = false;// To print all the pages without pagination in grid

        string filename = string.Empty;
        filename = "Report -" + DateTime.Now.ToString("yyyyMMddHHmmssfffff");
        Response.Clear();
        Response.Buffer = true;
        Response.ClearHeaders();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);          
        Maingrid.GridLines = GridLines.Both;
        Maingrid.HeaderStyle.Font.Bold = true;


        int x = Maingrid.Rows.Count;
        for (int i = 0; i < Maingrid.Rows.Count; i++)
        {
            GridViewRow row = Maingrid.Rows[i];
            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");
            row.BackColor = System.Drawing.Color.White;
        }
        Maingrid.RenderControl(hw);


        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        //style to format numbers to string
        Response.Output.Write("<h1>Merchant Registration report</h1>");
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM