Click here to Skip to main content
13,049,172 members (66,497 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am doing a project regarding Hotel Management System. In this project I am using ViewStates to store data within pages. Now I want to generate a Excel document(xls) by using the data stored in ViewState. I have stored the data into ViewState using Collections.

This is the code for the view state

protected Collection<restaurantresponsedto> Restaurants
if (ViewState[ViewstateRestaurantName] != null)
object o = ViewState[ViewstateRestaurantName]
return (Collection<restaurantresponsedto>)o
return null


ViewState[ViewstateRestaurantName] = value

And this is my Excel generation codes.

protected void Button1_Click(object sender, EventArgs e)
////RestaurantRequestDto restaurant = new RestaurantRequestDto()

Application xlapp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Application")
Workbook xlworkbook = (Workbook)xlapp.ActiveWorkbook
Worksheet xlworksheet = (Worksheet)xlworkbook.ActiveSheet

xlworkbook.SaveAs("csharp-Excel.xls", XlFileFormat.xlWorkbookNormal, XlSaveAsAccessMode.xlExclusive)
Range chart_range = xlworksheet.UsedRange
for (int i = 0 i < Restaurants.Count-1 i++)
for (int j = 0 j < 5 j++)
xlworksheet.Cells[i + Restaurants.Count, j + 1] = this.Restaurants.FirstOrDefault()


Your kind assistance will be appreciated.
Thanks in advance.
Posted 2-May-13 17:15pm
Not a question, not clear what help would you expect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100