export to excel but in new page

by Vahid 20. January 2009 05:42

in the last sub system which we were working on, there was requirement to export the result of a query which is from oracle database and is displayed in an asp.net data grid into excel file. but the component we are using for export to excel functionality gets the collection as input parameter and export the data to excel file and write the excel file into response object of the asp.net. this means once the functionality is invoked, a dialog box will be displayed asking Open the file, save the file of cancel the operation. no issue with save and cancel. but when they clicked on the open it opens the excel file in the same browser window where they were viewing the page and they close the window once they were through with viewing the excel file. this means they close the application.

now they needed the excel file be opened in a new window so that they will not loos their application states. what we did to do so is a little bit tricky. in the query page when user clicks on export to excel button we save the data to be exported in an asp.net session state and open a new browser on the user's machine using following code :

BaseList<QueryInvoiceResult> result = GetInvoices(p);

Session["exceldata"] = result;

Response.Write("<Script>window.open('ExcelExport.aspx')</Script>");

and we also moved the export to excel logic to the ExcelExport.aspx page

try

{

if (Session["exceldata"] != null)

{

BaseList<QueryInvoiceResult> result = (BaseList<QueryInvoiceResult>)Session["exceldata"];if (result.Count > 0)

{

ExportToExcel(ExportToExcelContent(result));

}

}

}

catch (Exception)

{

throw;

}

finally

{

Session[
"exceldata"] = null;

}

 

Tags:

.Net | Technical