HtmlString and SetStyle in Same cell

Hello Support,
Its not working if I use SetStyle and Htmlstring style in a same cell.
In Setstyle I have the following.

 Aspose.Cells.Style WraptextNonBold = wb.Styles[wb.Styles.Add()];
        WraptextNonBold.VerticalAlignment = TextAlignmentType.Top;
        WraptextNonBold.IsTextWrapped = true;
        WraptextNonBold.ShrinkToFit = true;
        WraptextNonBold.Font.IsBold = false;
        WraptextNonBold.Font.Size = 11;

In Html String I have this.

cell.HtmlString = "<Font Style=\"FONT-WEIGHT: bold;FONT-STYLE: italic;TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;COLOR: #ff0000;\">This is simple HTML formatted text.</Font>";

Please advise.
Thanks
Patrick

@patrickjjs

Thanks for using Aspose.Cells.

Please use Workbook.CreateStyle() and Cell.SetStyle() method to fix this issue. Please see the following sample code and its output Excel file for a reference.

Output Excel File.zip (5.7 KB)

C#

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

Cell cell = ws.Cells["C4"];
cell.HtmlString = "<Font Style=\"FONT-WEIGHT: bold;FONT-STYLE: italic;TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;COLOR: #ff0000;\">This is simple HTML formatted text.</Font>";

Aspose.Cells.Style WraptextNonBold = wb.CreateStyle();
WraptextNonBold.VerticalAlignment = TextAlignmentType.Top;
WraptextNonBold.IsTextWrapped = true;
WraptextNonBold.ShrinkToFit = true;
WraptextNonBold.Font.IsBold = false;
WraptextNonBold.Font.Size = 11;

//This line is important
cell.SetStyle(WraptextNonBold);

wb.Save("output.xlsx");

Hi Shakeel Faiz,
Thanks for your quick Reply. Its Not Working. Even you can see the Attached Excel Not comes with
FONT-WEIGHT: bold;
FONT-STYLE: italic;
TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;
COLOR: #ff0000

Bold, Italic, and Color Missing.

I need those Font setting and also Text Wrap setting.

Please advise.

Thanks

@patrickjjs

Please check the above code, it works fine. We have applied the style which says, the text should not be bold, size should be 11, text wrap should be applied and text vertical alignment should be top.

Aspose.Cells.Style WraptextNonBold = wb.CreateStyle();
WraptextNonBold.VerticalAlignment = TextAlignmentType.Top;
WraptextNonBold.IsTextWrapped = true;
WraptextNonBold.ShrinkToFit = true;
WraptextNonBold.Font.IsBold = false;
WraptextNonBold.Font.Size = 11;

If you do not want to apply style, then comment this line of code.

//This line is important
//cell.SetStyle(WraptextNonBold);

Hi Shakeel,
I Need to Apply Only Alignment Top , Text Wrapped True and ShrinkToFit True.
Then I need the Font Color and Font STYLE from HTML.

For example.

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

Cell cell = ws.Cells["C4"];
cell.HtmlString = "<Font Style=\"FONT-STYLE: italic;TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;COLOR: #ff0000;\">This is simple HTML formatted text.</Font>";

Aspose.Cells.Style WraptextNonBold = wb.CreateStyle();
WraptextNonBold.VerticalAlignment = TextAlignmentType.Top;
WraptextNonBold.IsTextWrapped = true;
WraptextNonBold.ShrinkToFit = true;

//This line is important
cell.SetStyle(WraptextNonBold);

wb.Save("output.xlsx");

The Font color is Red and it Should be italic and Underline. But The attached EXCEL dont have this format.

Please help.
ThanksOutput Excel File.zip (5.7 KB)

@patrickjjs

We will look into this further and update you asap.

Hi Shakeel,
Hope you reproduce/understand the issue what I mean here. Please give some alternative solution to achieve this.
Thanks

@patrickjjs

Please use explicit parameter i.e.

//This line is important, pass second parameter true for explicit setting
cell.SetStyle(WraptextNonBold, true); 

and it should fix your issue. Please see the full code below, its output Excel file as well as screenshot for a reference.

Output Excel File.zip (5.6 KB)

C#

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets[0];

Cell cell = ws.Cells["C4"];
cell.HtmlString = "<Font Style=\"FONT-STYLE: italic;TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;COLOR: #ff0000;\">This is simple HTML formatted text.</Font>";

Aspose.Cells.Style WraptextNonBold = wb.CreateStyle();
WraptextNonBold.VerticalAlignment = TextAlignmentType.Top;
WraptextNonBold.IsTextWrapped = true;
WraptextNonBold.ShrinkToFit = true;

//This line is important, pass second parameter true for explicit setting
cell.SetStyle(WraptextNonBold, true);

wb.Save("output.xlsx");

Screenshot

Hi,

we are facing the same problem using Aspose.Cells Version 17.7.0. Using the following sample code

Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet ws = wb.Worksheets[0];
Aspose.Cells.Cell cell = ws.Cells["C4"];
cell.HtmlString = @"<p>Normal</p><p><strong>BOLD</strong></p><p><span style=""text-decoration: underline;"">UNDERLINE</span></p><p><del>DELETE</del></p><p><span style=""color: #ff0000; font-size: 48px;"">BIG AND RED</span></p>";

Aspose.Cells.Style Wraptext = wb.CreateStyle();
Wraptext.IsTextWrapped = true;

//This line is important, pass second parameter true for explicit setting
cell.SetStyle(Wraptext, true);

wb.Worksheets[0].AutoFitColumns();
wb.Worksheets[0].AutoFitRows();

wb.Save("output.xlsx");

the formatting of the cell is lost. If we do not set the cell Style, the formatting of the cell is preserved but we have no word wrap which is desired.

Please give some advice.

Thanks,

@Matthias_Winzer

Thanks for using Aspose.Cells.

We have tested your code and generated the output Excel file and it looks good. Please see the following screenshot for your reference. If you still find the issue, then please provide us your expected output Excel file. We will look into this issue further and help you asap.

Download Link:
Output Excel File.zip (5.8 KB)

Screenshot: