HTML to Excel Worksheets

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.

Thank you for your rapid resolution.

@afitz,

You are welcome. We hope the updated version resolves your issue. If you have any additional questions or feedback, please don’t hesitate to reach out to us again.

Hello, considering HTML to Excel Worksheets is there a proper way to do Excel formulas- is it possible?
Something like:


<table border="1">
  <tr>
    <td>5</td>
    <td>10</td>
    <td x:Formula="=$A$1+$B$1"></td> <!-- Excel will compute this -->
  </tr>
</table>

@afitz,

I opened the Notepad/TextPad, pasted your HTML, and saved it as “test_formula1.html”. After manually opening the HTML file in MS Excel, I noticed that the formula in cell C1 is not displayed; the cell appears blank. However, the other cells, such as A1 and B1, contain values of 5 and 10 respectively. Please refer to the screenshot for clarification.
sc_shot1.png (28.7 KB)

Additionally, I have attached the HTML file within a zipped archive for your reference:
test_formula1.zip (290 Bytes)

You mentioned that it works correctly in MS Excel. Could you kindly provide us with a valid HTML file where the formula is properly displayed in the relevant cell? We would be happy to review it soon.

Hello! Thank you for your response.

  1. No I did not mention it works correctly
  2. We use Aspose.cells to export html to Excel
  3. We utilize basically a single file web page that gets exported through aspose.cells
  4. We are wondering if Aspose.cells has any cell attributes that trigger excel functions, or is there any correct way to code Excel functions that Aspose.cells will recognize and that will correctly and automatically render in the output considering this type of export?

@afitz,

Thanks for your feedback.

I further explored the Aspose.Cells APIs and found we have actually supported the feature (parse/import formulas from HTML) a few years ago. Please try the following sample code with the attached input HTML file. It works fine in the output XLSX file as I tested.
e.g.,
Sample code:

[C#]
Aspose.Cells.HtmlLoadOptions options = new Aspose.Cells.HtmlLoadOptions();
options.HasFormula = true;
Workbook workbook = new Workbook("e:\\test2\\test_formula12.html", options);
// Save the workbook
workbook.Save("e:\\test2\\out12.xlsx");
[JAVA]
com.aspose.cells.HtmlLoadOptions options = new com.aspose.cells.HtmlLoadOptions();
options.setHasFormula(true);
Workbook workbook = new Workbook("e:\\test2\\test_formula12.html", options);
// Save the workbook
workbook.save("e:\\test2\\out12.xlsx");

Please find attached the zipped archive containing both input HTML file and output XLSX file for your reference. Please note, to set a formula, you need to use the tags like the following. Also, in code you will set HtmlLoadOptions.HasFormula Boolean attribute to “true” when importing the HTML file via load options.
<td>=$A$1+$B$1</td>
files1.zip (6.4 KB)