tags:

views:

183

answers:

3

Hi, 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

A: 

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.

ck
oh then is there any other way to export my dataset to excel file? can you give me a sample code
Nagu
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.
ck
Or a CSV comma-delimited-file works well too.
kenny
A: 

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
A: 

This SO thread and answer link here is a good approach.

kenny