Hi,
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
{
get
{
if (ViewState[ViewstateRestaurantName] != null)
{
object o = ViewState[ViewstateRestaurantName]
return (Collection<restaurantresponsedto>)o
}
else
{
return null
}
}
set
{
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.