0

In my project when i click on a button it should generate excel file and in the middle of first line in excel file as a heading i should get as "my first excel file". I could not get it when i am trying with following code.Any ideas?. Thanks in advance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace listofdirectories
{
    public partial class ExportToExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentType = "application/ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=practise.xls");
            StreamWriter stw = new StreamWriter();
            HtmlTextWriter htw = new HtmlTextWriter(stw);
            stw.WriteLine("my first excel file");
            response.End();
        }

    }
}
prashanti
  • 31
  • 2
  • see [this question](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp) on how to generate an Excel file – Yoav Apr 12 '15 at 06:48
  • I searched for it but could not get how to resolve my error – prashanti Apr 12 '15 at 06:52

1 Answers1

2

You forgot to add the content to the response like this-

response.Write(stw.ToString());
response.End();

Also change your content type to this -

response.ContentType = "application/vnd.ms-excel";

Instead of using StreamWriter use StringWriter and change your code like this-

       Response.Clear();  
       Response.ClearContent();  
       Response.ClearHeaders();  
       Response.Charset = "";  
       string FileName = "filename.xls";  
       StringWriter strwritter = new StringWriter();  
       HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);        
       Response.Cache.SetCacheability(HttpCacheability.NoCache);  
       Response.ContentType ="application/vnd.ms-excel";    
       Response.AddHeader("Content-Disposition","attachment;filename=" + FileName); 
       Response.Write(strwritter.ToString());  
       Response.End();

To load the content in to the excel file, if recommend that you load the dataset with the data, then bind a grid view with that dataset and render the gridview as html after that fill the response with that rendered html like this-

  GridView gv = new GridView();
  gv.DataSource = dataset; //Your datasource from database
  gv.DataBind();
  Response.ClearContent();
  Response.Buffer = true;
  Response.AddHeader("content-disposition", "attachment; filename=filename.xls");
  Response.ContentType = "application/vnd.ms-excel";
  Response.Charset = "";
  StringWriter sw = new StringWriter();
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  gv.RenderControl(htw);
  Response.Write(sw.ToString());
  Response.Flush();
  Response.End();
Manik Arora
  • 4,702
  • 1
  • 25
  • 48
  • do you want the excel file to be saved to a path or want the file to be provided as a download? – Manik Arora Apr 12 '15 at 06:58
  • System.IO.StreamWriter class doesn't have a parameter less constructor see its msdn - https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx – Manik Arora Apr 12 '15 at 06:59
  • I want it to be downloaded. – prashanti Apr 12 '15 at 07:00
  • Please see my updated answer, this is the code which I have used earlier for the operation that you require – Manik Arora Apr 12 '15 at 07:04
  • yes the file is downloading if i change it to string reader.But i am getting data from first row first .I want to keep "My first excel file " as heading in the middle of first line in excel sheet.Can you help me in doing that. – prashanti Apr 12 '15 at 07:16
  • If you can see my updated answer I have specified that you have to fill a gridview control with the data and then fill the response with its rendered html. So the solution is- you have do format the gridview and make its format just like you want it to be displayed in the final downloadable excel file. Now you'll have to search on how to format the gridview like you want to be displayed in excel file. This should take you to the right path :) – Manik Arora Apr 12 '15 at 07:24
  • @prashanti, kindly mark the answer as accepted if it helped you, so that others may also refer to this for their same requirements – Manik Arora Apr 12 '15 at 14:11