0

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);
abc
  • 61
  • 1
  • 10
  • Short answer: You can't. You have no control over the file location of the client. And you are not creating an Excel file but a html page with an xls extension. Use a specialized library like EPPplus to create Excel files. – VDWWD Jun 16 '17 at 08:39
  • XLS file is a binary format, not a text one (possibly HTML format disguised as XLS?). You need to use third-party library to insert data into an Excel file then provide it to user (and even you can't decide where the file will be stored in client-side code). – Tetsuya Yamamoto Jun 16 '17 at 08:46
  • [Renamed HTML files will now open in Excel](http://www.infoworld.com/article/3106774/microsoft-windows/good-news-for-microsoft-office-renamed-html-files-now-open-in-excel.html). If OP can open it locally it'll work as a download too. – John Wu Jun 16 '17 at 09:35
  • could you please help me out as i am new to this – abc Jun 16 '17 at 09:37

2 Answers2

0

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.

John Wu
  • 50,556
  • 8
  • 44
  • 80
0

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)
    {

    }
Sivapriya
  • 29
  • 3
  • Hi sivapriya thankyou for you respons it doesnot match with my requirement could you please look th code that i have posted – abc Jun 16 '17 at 11:14
  • I have already gone through your code. There is no need to mention the Downloads path. Use the code which I have provided. That should work. – Sivapriya Jun 16 '17 at 11:26
  • i am not at all having main grid in my code i have multiple grids where i am trying to download all the multiple grids in single excel sheet i kept your code but its giving me errors near main grid – abc Jun 16 '17 at 11:28
  • Maingrid is the grid name I have used. You can replace with your grid name there. – Sivapriya Jun 16 '17 at 13:14
  • 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); repeat the same condition with all your three grids – Sivapriya Jun 16 '17 at 13:15