i’m calling
var blockCell = worksheet.Cells[
htmlBlock.OccupiedCells.StartRow,
htmlBlock.OccupiedCells.StartColumn
];
then just settings my html value
blockCell.HtmlString = html;
that equals to
<div style="font-family: Arial, sans-serif; max-width: 500px; margin: 20px;">
<h2 style="color: yellow;">Hello, this is a header</h2>
<p style="color: blue; line-height: 1.5;">This is an example of regular text in HTML. This could be any information: a description, news, instructions, or just sample text.</p>
</div>
And getting result without styles and spaces
image.png (6.3 KB)
Expected:
image.png (3.5 KB)
@bloodboilAaromatic
When you set the HtmlString
property of a cell in Aspose.Cells, it may not retain all the styles defined in your HTML. This is due to the limitations of HTML rendering in Excel, which may not support all CSS styles or HTML elements.
To work around this issue, consider the following approaches:
-
Use Rich Text Formatting: If you need to maintain specific styles, consider using rich text formatting for the cell instead of HTML. You can programmatically set different fonts, sizes, and colors.
-
Convert HTML to an Image: If the visual representation is critical, you could render the HTML to an image and then insert that image into the cell.
-
Use a Different Format: If feasible, convert your HTML to a format that retains styles better, such as DOCX, and then import that into your Excel file.
Here’s a simple example of how to set rich text formatting:
var blockCell = worksheet.Cells[htmlBlock.OccupiedCells.StartRow, htmlBlock.OccupiedCells.StartColumn];
var style = blockCell.GetStyle();
style.Font.Name = "Arial";
style.Font.Size = 12;
style.Font.Color = Color.Blue;
blockCell.SetStyle(style);
blockCell.PutValue("Hello, this is a header", true);
This approach allows you to control the appearance of the text more effectively.
@bloodboilAaromatic,
Thanks for the details.
I reproduced the issue as you mentioned by using the following sample code. I found Cell.HtmlString does not apply/implement styles for the inserted contents. For example, font text color is not taken care for the text portions.
// Create a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Access the cell where you want to insert rich text
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Set the HTML formatted string
cell.HtmlString = "<div style=\"font-family: Arial, sans-serif; max-width: 500px; margin: 20px;\">\n" +
" <h2 style=\"color: yellow;\">Hello, this is a header</h2>\n" +
" <p style=\"color: blue; line-height: 1.5;\">This is an example of regular text in HTML. This could be any information: a description, news, instructions, or just sample text.</p>\n" +
"</div>";
// Save the workbook
workbook.Save("e:\\test2\\out1.xlsx");
out1.zip (6.1 KB)
We require thorough evaluation of the issue. 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): CELLSNET-59016
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
Thanks a lot, i’m surprised a bit cause that common case
@bloodboilAaromatic,
Well, if we use HEX color code, it works to certain extent. Try the following HTML string:
cell.HtmlString = "<html><body> <p style=\"text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt;\"><span style=\"color:#000000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">Test </span><span style=\"color:#000000;background-color:#FFFF00;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">reden </span><span style=\"color:#FF0000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:normal;font-style:normal;\">terugsturen </span><span style=\"color:#000000;background-color:transparent;font-family:Calibri;font-size:8pt;font-weight:bold;font-style:normal;text-decoration: underline;\">besluit</span></p></body></html>";
Anyways, as we have logged a ticket for it, so let us evaluate it in details. We will keep you posted with updates on the ticket.
@bloodboilAaromatic ,
output.zip (6.1 KB)
We are pleased to inform you that your issue has been resolved. The fix will be included in an upcoming release (Aspose.Cells v25.10) that we plan to release in the first half of October 2025. You will be notified when the next version is released.
The result after applying the fix is attached for your review.
1 Like
Checked applied changes to a document, everything is alright for me except gap between h2 and p (u can see it in any online html editor), but almost sure that gap added by html markup itself, so i guess issue resolved.
@bloodboilAaromatic,
Thank you for your feedback. In your HTML string there is line break or new line char “\n
” after h2 and before “<p>
”. Anyways, we’re glad to hear that the output file generated by the internal fix has resolved your issue. We will keep you informed with updates regarding the new version as soon as it becomes available.