Hi,
I am going to export my datagrid data to excel . under click event i write that code
dynamic excel = AutomationFactory.CreateObject("Excel.Application");
but here i am getting the error called
This operation is not supported in the current context.
i set the Requiredelevated trust when running outside the browser in out of browser setting . still i am getting the error.
can anybody please help me . it is very urgent for me.
Thanks
P.Nagaraju
Loading
Jiteendra SampathiraoPosted Sep 14, 2011, 4:29 AM
Prabhu RajaPosted Sep 14, 2011, 3:35 AM
Add Namespaces First
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
//and code to export Datagrid to Excel File
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); //Give Excel File Name
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDataGrid.RenderControl(htmlWrite); // Give DataGrid Name Here
Response.Write(stringWrite.ToString());
Response.End();
--------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank you