Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Reply
ExportToExcel functionality in a Class file
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
Ashok N
14y
3k
1
Reply
Hi,
When I call the below function in ButtonDownLoad_Click like:- ExportToExcel(gv) ,I'm able to export the GridView contents to an xls file.
Please refer attached
ExportToExcel.rar
But there so many aspx pages with GridViews which I need to export to excel.
So I tried to keep the above export-coding in a Class as below
public class Class1 : System.Web.UI.Page
{
public void ExportToExcel(GridView Gv)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//this.ClearControls(Gv);
Gv.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
protected void ButtonDownLoad_Click(object sender, EventArgs e)
{
// ExportToExcel(gv); // function call within same page works fine..
Class1 c1 = new Class1();
c1.ExportToExcel(gv);
}
After Button click ,Getting error as below...
"Response is not available in this context."
Please suggest the solution so that above class can be used in all the pages (to avoid keeping code in every page).
Thanks in advance...
Attachment:
ExportToExcel.rar
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
System.UnauthorizedAccessException
PropertyGrid with ConfigurationElementCollection (cannot Add / Remove from collection)