Importing HTML with style sheets to Excel

Hi,

Do your product support importing HTML with stylesheets to Excel? Styles would include font style, border style, column width, number format etc.? Is that supported? I know html can be imported, but had some issues with importing using ImportHTMLTable() while style sheets are not recognized by your component. Let me know what best options to import an HTML document with embedded style


6 Replies

RS Ramya Sivakumar Syncfusion Team February 24, 2022 09:05 AM UTC

Hi Sunil, 

Greetings from Syncfusion. 

XlsIO supports importing HTML tables with the inline styles alone. HTML document with embedded styles or style sheets are not supported. 
 
Kindly refer to the following links to know more about the importing HTML table to Excel worksheet. 
 


Regards, 
Ramya. 



SU Sunil February 25, 2022 04:42 AM UTC

Thanks Ramya! I tried the second link you provided and it failed with below error: Used the same code and data file from github.

System.ArgumentException: 'rgb(255 is not a valid value for Int32. (Parameter 'htmlColor')'


Also, the data html file has lot of css class references. For example, 

<th class="X64" style="text-align:left;">


Where is the definition for X64? Can that be under the <style> </style> tag similar to a regular html/css file?


Thanks a lot!

Sunil



SU Sunil February 25, 2022 05:02 AM UTC

Also, is there a way I can indicate the custom cell formats (Number formats) for Currency, 100 separator etc. while importing html?


THanks!

Sunil



RS Ramya Sivakumar Syncfusion Team February 28, 2022 10:18 AM UTC

Hi Sunil, 
 
We are unable to reproduce the exception issue at our end.  
 
Please download the sample which we are tried at our end from the following link 
 
 
Query 
Response 
Where is the definition for X64? Can that be under the <style> </style> tag similar to a regular html/css file? 
Yes, we can add the definition for the X64 and X65 under the style tag. We will update the changes and republish the GitHub sample. 
As for now, please try the sample in the above link. 
Also, is there a way I can indicate the custom cell formats (Number formats) for Currency, 100 separator etc. while importing html? 
You can apply the Custom cell formats for the cells in the Excel file after importing the HTML. Please refer to the UG documentation from the below link. 
 
 
Regards, 
Ramya. 



SU Sunil February 28, 2022 11:31 PM UTC

Thanks Ramya for the detail! Able to use your recommendation for those. I have run into another issue, which I had pointed out earlier. Do you know why this happens?

'<', hexadecimal value 0x3C, is an invalid attribute character. Line 1, position 121.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: '<', hexadecimal value 0x3C, is an invalid attribute character. Line 1, position 121.

Source Error:

Line 399:                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(reportHTML)
Line 400:                Dim MStream As MemoryStream = New MemoryStream(byteArray)
Line 401:                worksheet.ImportHtmlTable(MStream, 6, 1)
Line 402:
Line 403:                ' Set Report Label to left aligned


Source File: C:\Users\sunil\source\repos\FMCWebAppNew\report\rBalanceRecapWithFuturesMonth.aspx.vb    Line: 401

Stack Trace:

[XmlException: '<', hexadecimal value 0x3C, is an invalid attribute character. Line 1, position 121.]
   System.Xml.XmlTextReaderImpl.Throw(Exception e) +73
   System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) +81
   System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr) +5382008
   System.Xml.XmlTextReaderImpl.ParseAttributes() +853
   System.Xml.XmlTextReaderImpl.ParseElement() +381
   System.Xml.XmlTextReaderImpl.ParseElementContent() +120
   System.Xml.XmlTextReaderImpl.Read() +45
   System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +57
   System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +21
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +133
   System.Xml.XmlDocument.Load(XmlReader reader) +109
   System.Xml.XmlDocument.LoadXml(String xml) +168
   Syncfusion.XlsIO.Implementation.HtmlStringParser.ParseHtml(String html, IRichTextString range) +140
   Syncfusion.XlsIO.Implementation.RangeImpl.set_HtmlString(String value) +91
   Syncfusion.XlsIO.Implementation.HtmlToExcelConverter.ParseTableColumn(XmlNode colNode, WorksheetImpl worksheetImpl, Int32 row, Int32 col) +263
   Syncfusion.XlsIO.Implementation.HtmlToExcelConverter.ParseTableRow(XmlNode rowNode, WorksheetImpl worksheetImpl, Int32& maxMergedRow, Int32 row, Int32 col, Dictionary`2 styles, Int32 tableRow, TextFormat format, Boolean& isColumnDisplay) +885
   Syncfusion.XlsIO.Implementation.HtmlToExcelConverter.ParseHTMLTable(String htmlText, WorksheetImpl worksheetImpl, Int32 row, Int32 col) +1549
   Syncfusion.XlsIO.Implementation.WorksheetImpl.ImportHtmlTable(Stream fileStream, Int32 row, Int32 column) +215
   FMC.rBalanceRecap.WriteReport




RS Ramya Sivakumar Syncfusion Team March 1, 2022 01:52 PM UTC

Hi Sunil, 
 
Greeting from Syncfusion. 

Syncfusion XlsIO depends on the XMLDocument object to load HTML string in which the “<” symbol is invalid. So an exception is thrown while using the “<” symbol.  
 
To overcome this, we suggest you change “<” as “&lt;” Please find the sample text below. 
 
Sample Text: 
<td>80 &lt; 100</td> 
 
Kindly try the suggestion and let us know if this helps. 
 

Regards, 
Ramya. 


Loader.
Up arrow icon