We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to store and retrive workbook object from ASP.net session

hi,

I want to know how can i store workbook in session variable or cashing variable and retrive from session variable. for example below is my code:

we are using asp.net and C# MSVS2010.

DataTable dataTable = null;
dataTable = Reports.GetReportPivotData();
Aspose.Cells.Workbook wb = new Workbook();
Aspose.Cells.Worksheet sheet1 = wb.Worksheets[0];
sheet1.Name = "Netting Data";
sheet1.Cells.ImportDataTable(dataTable, true, 4, 0, dataTable.Rows.Count, dataTable.Columns.Count, true, "d-mmm-yyyy");

GridWeb1.WebWorksheets.ImportExcelFile(wb.SaveToStream());
GridWeb1.WebWorksheets[0].Cells.Merge(3, 1, 1, 2);
GridWeb1.WebWorksheets.ActiveSheetIndex = 1;
Session["workbook"] = wb.Worksheets.ToString(); ??

how to retirve from session..

Hi Piyush,

Thanks for your posting and using Aspose.Cells for .NET.

You can convert your workbook object into byte array, then you can save the byte array in a session object, afterwards you can retrieve the byte array from session object and convert it into workbook object again.

Please see the following code for your reference. It converts the source xlsx file into byte array, then it converts the byte array into workbook again.

C#


string filePath = @“F:\source.xlsx”;


Workbook workbook = new Workbook(filePath);


MemoryStream ms = new MemoryStream();

workbook.Save(ms, SaveFormat.Xlsx);


ms.Position = 0;

byte[] bytesWorkbook = new byte[ms.Length];

ms.Read(bytesWorkbook, 0, bytesWorkbook.Length);


//Now you can save array of bytes into Session

//From Session, you can retrieve the bytes and convert it to workbook again using the following code


MemoryStream ms2 = new MemoryStream();

ms2.Write(bytesWorkbook, 0, bytesWorkbook.Length);


Workbook workbook2 = new Workbook(ms);


workbook2.Save(“out.xlsx”);