Aspose.Cells HtmlString assignment is losing font style attributes such as font-weight:normal

The string I am assigning into cell.HtmlString contains font tag with style attributes…
font-weight: normal
font-style: normal
text-decoration: none

in an attempt to override the styling inherited from the parent element’s style but the resulting HtmlString in the cell object removes those style attributes.

How can I specify these font style attributes at the cell object level using the HtmlString property?
Or, is there an alternative approach to set those style attributes via other cell object properties?

@bsant,

Could you please share HTML string that you are setting to the cell? Also, share your template Excel file (please zip it prior attaching). We will check your issue soon.

By the way, you may set formatting/style via Style object in code, see the document on font settings for your complete reference.

I will work on getting you sample code but its a bit complicated because we are using Aspose to generate HTML representation of an excel spreadsheet and then attempting to update the contents of a cell using Aspose.Cell’s HtmlString property.

In this case, after the HTML is generated, the HtmlString property is created as:

<Font Style=“FONT-WEIGHT: bold;FONT-STYLE: italic;FONT-FAMILY: arial;FONT-SIZE: 10pt;COLOR: #ff0000;Background-Color: #ffff99;”>Please see below…</Font>

Note that the Font styling here is being added to this html Font element based on a parent td element’s class being a shared style created during the html generation of the spreadsheet.

We attempt to assign the following into the cell’s HtmlString property:

<font style=“font-style: normal; font-weight: normal; text-decoration: none;”>Please see below.</font>

The resulting value in the HtmlString property is:

<Font Style=“FONT-FAMILY: arial;FONT-SIZE: 10pt;COLOR: #ff0000;Background-Color: #ffff99;”>Please see below.</Font>

Note that it did get rid of the original FONT-WEIGHT and FONT-STYLE attributes. However, the parent td elements shared class is being inherited and the display still shows the cell’s content as bolded and as italic.

It seems that our assignment html string with the “font-style: normal” and “font-weight: normal” attributes into the HtmlString property removes the original attributes instead of explicitly setting the style attributes our string is specifying. Without those being explicitly specified in the cell’s html, the inherited styles from the parent element takes control.

I suspect it has something to do with those attribute values being the css default values and maybe the logic thinks it is enough to simply remove the original attributes instead of explicitly using our updated attribute values.

Note that the class assigned to the parent td element is derived from the Aspose Workbook method “GetStyleInPool” and, in this case, results in the following shared class:

.style74
{
text-align: general;
vertical-align: top;
white-space:normal;word-wrap:break-word;
background:#FFFF99;
font-size: 10pt;
font-weight: 700;
font-style: italic;
font-family: “arial”;
border-top: 1px solid #000000;
border-right: 1px solid #000000;
border-bottom: 1px solid #000000;
border-left: 1px solid #000000;
}

@bsan,

We tried to reproduce the issue based on your description, but did not get the desired results. However, you can also share the original excel file and sample code, we will analyze it further.

I have tried using the following for the cell but did not work…

            Aspose.Cells.Style cellStyle = wipCell.GetStyle();
            cellStyle.Font.IsBold = false;
            cellStyle.Font.IsItalic = false;
            cellStyle.Font.Underline = FontUnderlineType.None;
            wipCell.SetStyle(cellStyle, true);

So, your testing of assigning an html string to the cell’s HtmlString property is explicitly setting the “font-style” and “font-weight” attributes on a font tag/element when the html string has those attributes set to “normal” ???

@bsant,

Could you please also share your sample Excel file and mention upon which cell you are trying to override (font) style attributes via the code segment and is not working? We will check it soon.

TestDraft.zip (10.2 KB)

Single cell spreadsheet has the content that is having the issue.

@bsant
If the font is regular, we will not output FONT-STYLE to make the output HTML as concise as possible. And when parsing Cell. HtmlString separately, these attributes have no meaning.

Do you want to output FONT-WEIGHT and FONT-STYLE attributes anyway, even if they are meaningless, because you need to embed this HTML string into existing HTML?

The problem occurs because the html structure is

table
tr
td class=“style74”
div
font

style74 is a class derived/generated by the Aspose GetStyleInPool call.
When Aspose generates the html for the font tag/element, it is specifying the font-weight: bold and font-style:italic that is specified in the style74 class on the td element.

So, when the updated html string explicitly says “font-weight: normal” or “font-style: normal”, the Cell.HtmlString assignment does not explicitly set them and actually strips them out. As you said, since “normal” is a default value for those attributes, it “can” make sense to remove them for the sake of being concise. The issue becomes that the parent td element has the class=“style74” which specifies font bold/italic and since the descendent element of the font tag inherits from the parent, the html rendering leaves the content of the font tag as bolded and italicized. If the parent td element didn’t have a class that specified bold/italic font styling, it wouldn’t be a problem.

That derived/generated class of “style74” has other css attributes like alignment, border, padding, etc… that is necessary at the td level for proper html rendering of the overall html table, rows and cells. Unfortunately, it also includes font styling that is initially explicitly added to the font tagged element if other than normal. But, in this case, the user is modifying the cell and wants to remove the bold/italic but the inheritancy of the font styling from the td element’s class prevents that from an html rendering perspective.

@bsant,

Thanks for the details.

We will evaluate and get back to you soon.

A different twist.

If the HtmlString property originally generates the “FONT-WEIGHT: bold” (derived from the td element’s style74 class) and I give try to assign an html string without any “FONT-WEIGHT” style attribute (as opposed to a “FONT-WEIGHT: normal”), the resulting HtmlString persists the “FONT-WEIGHT: bold” style attribute. I presume it is getting that from the Cell properties tracking that ultimately the “styling” is coming from that shared class.

@bsant,

Thanks for providing your findings.

Please give us a little time so that we can analyze every aspect, including this one, in detail before we can provide further updates on it.

@bsant
Could you create a sample project with template files and expected files? We will check them soon.