0

I'm trying to create an xls files using c# like this

protected void ExportToExcel(DataSet dset, int TableIndex, HttpResponse Respone, string Felename)
    {
        Respone.Clear();
        Respone.Charset = "";
        Respone.ContentType = "application/vnd.ms-excel";
        Respone.AddHeader("content-disposition", "attachment; filename=" + Felename + ".xls");
        System.IO.StringWriter sw = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        GridView gv = new GridView();
        gv.DataSource = dset.Tables[TableIndex];
        gv.DataBind();
        gv.RenderControl(hw);
        Respone.Write(sw.ToString());
        Respone.End();
    }

It is working fine for me. But it is opening that created xls file after successfull completion. But i want to save the file in my folder without opening and asking my permision.

How can i save automatically..

Thanks in advance

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
Nagu
  • 4,954
  • 14
  • 52
  • 67
  • This SO thread and [answer link here](http://stackoverflow.com/questions/871115/what-is-the-best-and-fastest-way-to-write-into-excel-file-using-c/871142#871142) is a good approach. – kenny Oct 09 '09 at 14:19

2 Answers2

0

Change the content-type to application/x-unknown - depending on the browser this should only offer a save option as it won't recognise it as an Excel file.

Also, be aware that you are actually just saving an HTML file with an Excel extension, it isn;t a true Excel file.

cjk
  • 45,739
  • 9
  • 81
  • 112
  • oh then is there any other way to export my dataset to excel file? can you give me a sample code – Nagu Oct 09 '09 at 10:56
  • You either need to use Excel interop, or buy a third party component to do this. The HTML way will work to a certain degree. – cjk Oct 09 '09 at 12:25
0

SpreadsheetGear for.NET can save to an Excel workbook from a DataTable.

See the DataTable to Excel Workbook C# or VB sample on the SpreadsheetGear ASP.NET Excel Reporting Sample page here.

You can download a free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
  • 7,077
  • 1
  • 31
  • 31