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

Free Support Forum - aspose.com

JPEG images using byte array in aspose cells

Hi,

Could you please let me know, if there’s a way to insert jpeg image which is available in byte array format into excel sheet using aspose cells java API?

Thanks for your response!

Regards,
Balakumar

@BalakumarSeethapathy

Thanks for considering Aspose APIs.

Please see the following sample code, its sample image and output Excel file for a reference. Please read the comments inside the code.

The code first reads the image in byte[]. Then it converts byte[] into ByteArrayInputStream and finally it adds the image in a workbook using Aspose.Cells.

Download Link:
Sample Image and Output Excel File.zip (1.6 MB)

Java

//Read image into byte array
File f = new File(dirPath + "sample-image.png");
int len = (int)f.length();

byte[] b = new byte[len];

FileInputStream fis = new FileInputStream(f);
fis.read(b);
fis.close();

//-------------------------------------
//-------------------------------------

//The code starts from here.

//-------------------------------------
//-------------------------------------

//This byte array contains image
byte[] imgBytes = b;

InputStream insImg = new ByteArrayInputStream(imgBytes); 

Workbook wb = new Workbook();

Worksheet ws = wb.getWorksheets().get(0);

ws.getPictures().add(0, 0, insImg);

wb.save(dirPath + "output.xlsx");