Free Support Forum - aspose.com

Export to Excel for user

Hi

Once I have a grid displayed on a web page, my users want a button that will export the grid as seen on the web page into an Excel file for them to save locally i.e. I don't want to save to a file or stream on the server side.

How do I do this?

Thanks.

Hi,

Yes you may do it. You can write lines of code in GridWeb_SaveCommand event handler. So when you click on the save button(icon) on the control you can save it locally on the client.

Example:

private void GridWeb1_SaveCommand(object sender, System.EventArgs e)
{
// Generates a temporary file name.
string filename = System.IO.Path.GetTempPath() + Session.SessionID + ".xls";
// Saves to the file.
this.GridWeb1.WebWorksheets.SaveToExcelFile(filename);
// Sents the file to browser.
Response.ContentType = "application/vnd.ms-excel";
//Adds header.
Response.AddHeader( "content-disposition","attachment; filename=book1.xls");
// Writes file content to the response stream.
Response.WriteFile(filename);
// OK.
Response.End();
}
Thank you.

Great, thanks Amjad.

That worked a treat! :)