I have this annoying problem; while executing the code below, I always get a Save As dialog instead of being able to save at the specified location (C:\temp\test1234.xls). Everything goes well if I navigate to some location, give the file a name - however
I can't figure out why I just can't skip the dialog ...
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var ds = new DataSet();
var dt = new DataTable("TableName _For Sheet1");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Rows.Add("Value1", "Value2");
ds.Tables.Add(dt);
ExcelExport exp = new ExcelExport();
exp.ExportDataSetToExcel(ds, "c:\\temp\\test1234.xls");
}
}
public class ExcelExport
{
public void ExportDataSetToExcel(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
}
As you are sending the content to the user through the "response", user will get the dialog as it is on the client side and is out of your hand. If you are trying to save the file on the server, then you need to use the IO methods like in this link:
kowalsky
Member
69 Points
54 Posts
save dataset to Excel with ASP.NET
Apr 28, 2010 08:54 PM|LINK
hi all,
I have this annoying problem; while executing the code below, I always get a Save As dialog instead of being able to save at the specified location (C:\temp\test1234.xls). Everything goes well if I navigate to some location, give the file a name - however I can't figure out why I just can't skip the dialog ...
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var ds = new DataSet(); var dt = new DataTable("TableName _For Sheet1"); dt.Columns.Add("col1"); dt.Columns.Add("col2"); dt.Rows.Add("Value1", "Value2"); ds.Tables.Add(dt); ExcelExport exp = new ExcelExport(); exp.ExportDataSetToExcel(ds, "c:\\temp\\test1234.xls"); } } public class ExcelExport { public void ExportDataSetToExcel(DataSet ds, string filename) { HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; // set the response mime type for excel response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); // create a string writer using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // instantiate a datagrid DataGrid dg = new DataGrid(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); response.Write(sw.ToString()); response.End(); } } } }Thanks,
kowalsky
nomercy007
Participant
1561 Points
297 Posts
Re: save dataset to Excel with ASP.NET
Apr 29, 2010 09:29 AM|LINK
As you are sending the content to the user through the "response", user will get the dialog as it is on the client side and is out of your hand. If you are trying to save the file on the server, then you need to use the IO methods like in this link:
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=400
niralpatel
Contributor
2391 Points
452 Posts
Re: save dataset to Excel with ASP.NET
Apr 29, 2010 09:36 AM|LINK
Hi,
This is u can do with two steps only
http://www.codeproject.com/KB/office/datasettoexcel.aspx
Please Mark As Answered If This Really works For You
or Let me know for Further help.
Thanks
Niral Patel
(INDIA)