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

Free Support Forum - aspose.com

Make Workbook serializable?

Hello,


I noticed that opening an excel file can take quite long in comparison to the time it takes to actually work with it through the Aspose.cells interface. Therefor, I would like to open an excel file only once (at best) and store the Workbook instance of it in shared memory using a tool like Memcached (http://code.google.com/p/memcached/). But this requires Workbook to be serializable, which it isn’t at the moment. Is there a possibility to add serializability to Workbook?

Kind regards,
DC

Hi,

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

You can save your Workbook object into byte array and then can cache it, later you can again recreate your Workbook object from the byte array. Let us know if it works in your case.

Workbook contains all excel objects and below are utility functions to serialize/deserialize workbook object. The code first saves the workbook into byte array and then recreate the workbook from byte array.

C#

public static void TestSerialization()

{

string filePath = @“f:\downloads\test.xls”;

Workbook book = new Workbook(filePath);


byte[] bytesBook = SerializeWorkbook(book);


Workbook newBook = DeSerializeWorkbook(bytesBook);


newBook.Save(filePath + “.out.xls”);

}


public static byte[] SerializeWorkbook(Workbook workbook)

{

//Create a memory stream

MemoryStream ms = new MemoryStream();


//Save the workbook which contains all excel objects

//into memory stream

workbook.Save(ms, SaveFormat.Excel97To2003);


//Read bytes from memory stream

ms.Position = 0;

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

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


return bytesWorkbook;

}


public static Workbook DeSerializeWorkbook(byte[] bytesWorkbook)

{

MemoryStream ms = new MemoryStream();

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


Workbook workbook = new Workbook(ms);


return workbook;

}

Thanks man! Works like a charm!

is something like “SerializeWorkbook” available in Java?

@dvector,

Thanks for your query.

ByteArrayInputStream and ByteArrayOutputStream can be used for this purpose. Here is an example where a workbook is saved to byte array and then same byte array is used to create a workbook. You may please give it a try and provide your feedback.

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getCells().get("P30").putValue("This is sample data.");

// Save the workbook in memory stream
ByteArrayOutputStream baout = new ByteArrayOutputStream();
workbook.save(baout, SaveFormat.XLSX);

// Get bytes and create byte array input stream
byte[] bts = baout.toByteArray();
ByteArrayInputStream bain = new ByteArrayInputStream(bts);

LoadOptions opts = new LoadOptions(LoadFormat.XLSX);
workbook = new Workbook(bain, opts);

can we store this in local drive ? because it store in server memory only

@ameet8486,

See the document with examples on how to save files to some filepath/storage location for your reference:
https://docs.aspose.com/display/cellsnet/Different+Ways+to+Save+Files#DifferentWaystoSaveFiles-SavingFiletoaStream

Hope, this helps a bit.

I mean i want to store workbook object in local drive , dont want to store xlxb file
As my data is very huge like more then 150 MB , if i call workbook.save() method system get crashed , so we are planing to store workbook object instant of xlxb file ,

please suggest if you have better option to deal with that much huge data .

my requirement is create formula based xlxb file(data set would be 1.5 crore ) and save in local folder and same file i need to read during show case to client

I am using aspose java

@ameet8486,

Well, Aspose.Cells is a server side MS Excel management library. We also provide a light weight approach, i.e., LightCells API in Aspose.Cells APIs where you may read/write data in light weight mode. Aspose.Cells provides the LightCells API, mainly designed to manipulate cell data one by one without building a complete data model block (using the Cell collection etc.) into memory. It works in an event-driven mode. See the document with examples for your reference:
https://docs.aspose.com/display/cellsjava/Using+LightCells+API

Moreover, if you are only concerned about memory, Aspose.Cells provides an option e.g MemorySetting.MemoryPreference for memory performance consideration, so you may try it. Please see the topic for your complete reference on how to optimize memory usage while reading/rendering bigger files having large data sets etc.:
https://docs.aspose.com/display/cellsjava/Optimizing+Memory+Usage+while+Working+with+Big+Files+having+Large+Datasets

Hope, this helps a bit.