HTML to Excel Worksheets

I also forgot to note, as I have just realized looking at resolved issues CELLSNET-52159 and CELLSNET-53157, but we are using Aspose.cells for Java not for .NET, so I wonder if those fixes also need to be for CELLSJAVA…?

@afitz,

The fixes are also included in Java version in parallel as we port Java version from .NET codebase. Please try latest available version of Aspose.Cells for Java (v23.7).

Let us know if you still find these issues.

The issues you have found earlier (filed as CELLSJAVA-44907) have been fixed in Aspose.Cells for Java 23.8.

Hello Aspose team!
We have upgraded to v23.1 of Aspose Cells, and there were so many great fixes. Thank you!
Considering images in cells, we can pass them into the export as an HTML img tag with the source as base64 but they are tricky to resize in relationship to the cell boundary. Is there something in particular that we should do for formatting images in relationship to the cell boundary and sizing of the cell? Can the cell size responsively with the image or vice versa? Any tricks or tips for images?

@afitz,

In MS Excel file formats (source XMLs), images are stored as separate entities and are not linked or embedded into worksheet cells directly. Therefore, you need to evaluate the size of the image and the cell’s width/height where you are going to insert it yourself.

@afitz
Would you like to provide sample files and expected result files? We will check it soon.

Hello - thank you for responding to all my questions.
Has anything changed given Excel’s new capabilities to insert images into cells since amjad.sahi response in Jan 2024 ?
For example when I insert an image into an Excel cell it will resize with the cell. But I cannot figure out a way in my html/css export to Excel through Aspose.cells for this capability.
Has Aspose.cells made this functionality available and if so any html/css examples you can point me to?
Currently we must size the cell width and height and image width and height accordingly and with ample margin.

@afitz,

Aspose.Cells supports the feature of placing or embedding an image into a cell. You can try to use the Cell.setEmbeddedImage() method to embed an image directly into the cell.

Hi @amjad.sahi -
We are doing it from the html of a template, so it would need to be something we do with the html and css. Perhaps in our backend we could also add code that if an img is found in the html, set it in the cell as you have described?

And to be certain when someone uses the method you have described, is the image able to responsively size in cell?

@afitz
Please refer to the following example code to add embedded image in cell and check the attachment, you can see that the inserted image has adapted to the size of the cell. result.zip (20.9 KB)

Workbook book = new Workbook();

Cells cells = book.getWorksheets().get(0).getCells();
Cell b6 = cells.get("B6");

byte[] imageBytes = Files.readAllBytes(Paths.get(filePath + "test.png"));
b6.setEmbeddedImage(imageBytes);

book.save(filePath + "out_java.xlsx");

Hope helps a bit.

@afitz
Of course, you can also refer to the following example code to directly convert the images into embedded images using Picture.placeInCell() method. Please refer to the attachment. embeddedImages.zip (37.2 KB)

Workbook book = new Workbook();

Worksheet sheet = book.getWorksheets().get(0);

Cells cells = sheet.getCells();
Cell b6 = cells.get("B6");

byte[] imageBytes = Files.readAllBytes(Paths.get(filePath + "test.png"));
b6.setEmbeddedImage(imageBytes);



PictureCollection pictures =	sheet.getPictures();
pictures.add(8, 5, filePath + "test.png");		   
pictures.add(12, 5, filePath + "test.png");
   	    
book.save(filePath + "out_java.xlsx");

int pictureCount = pictures.getCount();
for (int i = pictureCount - 1; i >= 0; i--)
{
	pictures.get(i).placeInCell();
	
}

book.save(filePath + "out_java2.xlsx");

Hope helps a bit.

@afitz
If you need to load HTML and set all images as embedded images, please refer to the following example code. Please refer to the attachment. result.zip (28.7 KB)

Workbook book = new Workbook();

Worksheet sheet = book.getWorksheets().get(0);			

sheet.getPictures().add(8, 5, filePath + "test.png");		   
sheet.getPictures().add(12, 5, filePath + "test.png");
   	    
book.save(filePath + "out_java.html");		

// load html 
Workbook workbook = new Workbook(filePath + "out_java.html");

WorksheetCollection worksheets = workbook.getWorksheets();
int worksheetCount = worksheets.getCount();
for (int s = 0; s < worksheetCount; s++)
{
	Worksheet worksheet = worksheets.get(s);			

	PictureCollection pictures =	worksheet.getPictures();
	//set all images as embedded images
	int pictureCount = pictures.getCount();
	for (int i = pictureCount - 1; i >= 0; i--)
	{
		pictures.get(i).placeInCell();
		
	}
	
}


workbook.save(filePath + "out_java.xlsx");

@afitz ,

We are considering adding an attribute to the HTML style, such as "mso-embedded-picture: true", to explicitly indicate embedded images. Then we can check whether this image should be embedded when converting HTML to Excel.

Thank you all for your answers I am sharing with my team.

  • We really like the option of being able to trigger it from the HTML so if the mso-embedded-picture option gets added we will be looking out for what version.

  • For this code what is the Aspose.cells version cutoff we would need?
    // load html
    Workbook workbook = new Workbook(filePath + “out_java.html”);

WorksheetCollection worksheets = workbook.getWorksheets();
int worksheetCount = worksheets.getCount();
for (int s = 0; s < worksheetCount; s++)
{
Worksheet worksheet = worksheets.get(s);

PictureCollection pictures =	worksheet.getPictures();
//set all images as embedded images
int pictureCount = pictures.getCount();
for (int i = pictureCount - 1; i >= 0; i--)
{
	pictures.get(i).placeInCell();
	
}

}

Thank you

@mlyra
Thank you for your feedback. Once the mso-embedded-picture option is supported, we will notify you promptly.

Picture.placeInCell() method is supported starting from version 24.7. If you need to use it, you need to update the version. Please refer to the following document.

1 Like

@afitz
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSJAVA-46335

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

@mlyra
We are pleased to inform you that mso-embedded-picture option will be supported in our upcoming release (Aspose.Cells for Java v25.4) that we plan to release in the first half of April 2025. You will be notified when the next version is released.

1 Like

Oh, wow, thats great news. I am always impressed how quickly your team implements new features!
A couple edge cases I am curious about - if the cell has a mix of text and images, say a couple sentences, an image, a couple sentences, an image… will the solutions you provided (Picture.placeInCell() method or upcoming mso-embedded-picture) be able to handle that, or will it strictly be one image per cell for the solutions you have provided?

Thank you!

@afitz
When inserting an image into Excel, if you select ‘place in cell’, the cell will no longer be able to input text. If there is already text in the cell, when you insert an image and select ‘place in cell’, the text data will be deleted. You can manually insert images for testing in Excel. You will find that either data or images cannot be placed in a cell at the same time.

1 Like

The issues you have found earlier (filed as CELLSJAVA-46335) have been fixed in Aspose.Cells for Java 25.4.