Export data from dataset to excel

creator picture creator · Apr 26, 2011 · Viewed 64.2k times · Source

I am trying to export data from dataset to excel and save it directly to a given path without giving me the option to open,save or cancel.

Answer

Developer picture Developer · Apr 26, 2011

Check this DataSetToExcel

and c# (WinForms-App) export DataSet to Excel

In the first link change the code as follows:

Remove the all code that initially starts and try the following

using (StringWriter sw = new StringWriter("Your Path to save"))
{
  using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  {
    // instantiate a datagrid
    DataGrid dg = new DataGrid();
    dg.DataSource = ds.Tables[0];
    dg.DataBind();
    dg.RenderControl(htw);
  }
}