public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
//add ur selected columns
context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
//---------
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
context.Response.End();
}
A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
sunitashirsa...
Member
300 Points
294 Posts
Export dataset to excel with specific coulmns
Mar 01, 2012 06:06 AM|LINK
Hi everyone,
I am exporting dataset which is passing by session have all column of datatable.
but when i am exporting to excel i dnt want all column to be export. Some of them should be export.
Here is my code..
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
string filename = "File" + "-" + DateTime.Now.ToShortDateString();
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.ClearContent();
response.ContentType = "application/vnd.ms-excel";
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];
GridView g = new GridView();
g.DataSource = ds;
g.DataBind();
g.RenderControl(htw);
response.Write(sw.ToString());
try
{
response.End();
}
catch (System.Threading.ThreadAbortException err)
{
if (err.Message != "Thread was being aborted.") Response.Write("Error occur while exporting excel.<br><br><b>For more information:</b><br>" + err.Message);
}
}
}}
usman400
Contributor
3513 Points
721 Posts
Re: Export dataset to excel with specific coulmns
Mar 01, 2012 06:20 AM|LINK
Play with data set like:
and after that, bind the GridView
venkatmca008
Participant
1810 Points
341 Posts
Re: Export dataset to excel with specific coulmns
Mar 01, 2012 06:26 AM|LINK
hi..please try this and
public static void ExportToSpreadsheet(DataTable table, string name) { HttpContext context = HttpContext.Current; context.Response.Clear(); foreach (DataColumn column in table.Columns) { context.Response.Write(column.ColumnName + ";"); } context.Response.Write(Environment.NewLine); foreach (DataRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { //add ur selected columns context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";"); //--------- } context.Response.Write(Environment.NewLine); } context.Response.ContentType = "text/csv"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv"); context.Response.End(); }Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
nuttynuts
Member
92 Points
36 Posts
Re: Export dataset to excel with specific coulmns
Mar 01, 2012 06:40 AM|LINK