Free Support Forum - aspose.com

Unable to render html in a cell

Hi,

In my application i m trying to put HTML in a single cell. The HTML string can have an anchor tag, a header, an unordered list and line breaks. when i use the htmlstring property of cells to store the HTML string, it doesn’t render it the way excel does.
my sample code is:

Workbook workbook = new Workbook(templatepath);
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells[1,1].HtmlString = "

header1

sample site
    list
  • item1
  • item2
";
workbook.Save(memoryStream, SaveFormat.Xlsx);

Please help me with this.

@satishojhaa,

After an initial test, using the following sample code, I got the issue as you mentioned:
e.g
Sample code:

   Workbook workbook = new Workbook(); 
    Worksheet worksheet = workbook.Worksheets[0]; 
    worksheet.Cells[1,1].HtmlString = "<html><h1>header1</h1> <a>sample site</a> <ul> list <li>item1</li><li>item2</li> </ul></html>"; 
    workbook.Save("e:\\test2\\out1.xlsx", SaveFormat.Xlsx); 

I also found that if I put the HTML into a separate HTML file (attached). When opening the file into MS Excel, MS Excel does not render bullets or list items, although it renders header fine. I guess the bullets or list items are not supported when parsing HTMLs either. The HTML tags which MS Excel cannot handle may not be supported by Aspose.Cells APIs.

I have logged a ticket with an id “CELLSNET-46012” for your issue. We will look into it soon.

Once we have an update on it, we will let you know here.
file1.zip (231 Bytes)

Hi @Amjad_Sahi,

While debugging the HtmlString property we got to know the issue. When we assign any html string to a cell through HtmlString property, it replaces the html tags with font tag and set attributes for the same. By default the font size for header is getting fixed 12pt and font weight is not set to bold.

Here is the example

string that we are assigning = "

Major Heading

\n\n

Heading

\n\n

Minor Heading

\n\n

Italic StyleBold Style\nLink Title (http://en.wikipedia.com/wiki/Markdown)\n* item 1\n* item 2\n * item 2a\n * item 2b\n * item 2bi\n *item 2bi\n * item 3

\n\n
    \n
  1. item 1
  2. \n
  3. item 2\n
      \n
    • item 2a
    • \n
    • item 2b
    • \n
  4. \n
\n"

The value returned by cell.HtmlString =" <Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>Major Heading<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>Heading<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>Minor Heading<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>Italic Style<Font Style=“FONT-WEIGHT: bold;FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>Bold Style<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>
Link Title (http://en.wikipedia.com/wiki/Markdown)
* item 1
* item 2
 * item 2a
 * item 2b
  * item 2bi
   item 2bi
 
item 3<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>


<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>
    1. <Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>item 1<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

    2. <Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>item 2
<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>
        <Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>item 2a<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

        <Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>item 2b<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

<Font Style=“FONT-FAMILY: Arial;FONT-SIZE: 12pt;COLOR: #000000;”>

".aspose.zip (558 Bytes)

Please refer to the file attached.

@satishojhaa,

Thanks for the sample HTML string.

We did notice your recent findings. I have recorded it against your issue “CELLSNET-46012” into our database. Please spare us little time, we will evaluate your issue soon.

Once we have any new information on your issue, we will share it with you.

@satishojhaa,

I am afraid, Cell.HtmlString only supports simple rich formatted text with HTML tags (like the returned string value by Cell.HtmlString) and does not support complete HTML file(s). If you want to import HTML file, please use new Workbook() constructor where you may load the HTML file into Workbook object.
e.g
Sample code:

HTMLLoadOptions opts = new HTMLLoadOptions(LoadFormat.Html);
Workbook workbook  = new Workbook(strFileName, opts);

Hope, this helps a bit.

@satishojhaa,

I am afraid, Cell.HtmlString only supports simple rich formatted text with HTML tags (likes returned string value by Cell.HtmlString and does not support HTML file. If you want to import HTML file, please use new Workbook() constructor to load the HTML file via Workbook object.