Free Support Forum - aspose.com

Convert to byte array after creating a excel file

Hi,

I have the requirement of createing excel file using aspose cells.once created i want to send an email by attaching the created excel file.

Witthout saving the file i want to send an email with attachment. In order to send an attachment i need a byte array of the file created.

Please tell me how can i convert the byte array from the created workbook .

Thanks

Mamatha

Hi Mamatha,

May the following sample code help you for your requirement.

Sample code:

Workbook workbook = new Workbook();
// ........................
//your code goes here
//.........................

MemoryStream ms = new MemoryStream();
workbook.Save(ms,FileFormatType.Default);
ms.Seek(0, SeekOrigin.Begin);

byte[] buffer = new byte[(int)ms.Length];
buffer = ms.ToArray();

..............

Thank you.

Hi Amjad,

Thanks for solution, but I don't know MemoryStream class is part of which Java Api.

Can you please tell me about MemoryStream class. So that i can try how it works.

Thank you.

Hi,

Well, MemoryStream is class in System.IO namespace in .NET. But, since you are using Java version of the component, so, kindly ignore my previous post and refer to the following sample code. I use ByteArrayOutputStream class from java.io package.

Sample code:

Workbook workbook = new Workbook();
// ........................
//your code goes here
//.........................

ByteArrayOutputStream ms=new ByteArrayOutputStream();
workbook.save(ms, FileFormatType.DEFAULT);
byte b[] = ms.toByteArray();

Thank you.

Hi Amjad,

Thanks for the solution. it works fine.

Assalam-o-alikum,

i want to convert an excel file into bytes and want to create small files from that and these file can be merged to get the original one back plz help me. and i did not get workbook.save() in jxl lib.

Hi,


Well, I am afraid, I can only help you regarding our product: Aspose.Cells for Java (Latest Version).

Using Aspose.Cells for Java API, you may convert Excel file to byte array, see the sample code below:

Sample code:

Workbook workbook = new Workbook();
// …
//your code goes here
//…

ByteArrayOutputStream ms=new ByteArrayOutputStream();
workbook.save(ms, FileFormatType.EXCEL97TO2003);
byte b[] = ms.toByteArray();


We recommend you to browse Aspose.Cells for Java component that covers the the standard and advanced features that MS Excel 97 - 2010 provides, see the documentation:

https://docs.aspose.com/display/cellsjava/Home


Thank you.



thank you amjad bhai!