Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0
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 Restaurants
if (ViewState[ViewstateRestaurantName] != null)
object o = ViewState[ViewstateRestaurantName]
return (Collection)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 18:15pm
Sergey Alexandrovich Kryukov at 3-May-13 0:06am
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)

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2015
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