Click here to Skip to main content
15,498,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to insert a custom HTML table from a code to excel. However, it does not work from the backend side. I always get a raw table string in the cell; however, it works when I open a file and manually copy and paste the cell's value somewhere else, then I get my custom table.

I've been searching for a solution for a few days now, but nothing works for me.

I will appreciate any suggestions.

What I have tried:

string htmlTable = "<table class=\"MsoTableGrid\" border=\"1\" cellspacing=\"0....TABLE CONTENT ETC. </table>

using (var package = new ExcelPackage(file))
{
    var worksheet = package.Workbook.Worksheets.Add("ReportNamePlaceHolder");

    
    worksheet.Cells["A1"].IsRichText = true ;
    worksheet.Cells["A1"].RichText.Text = htmlTable;
   
   
    worksheet.Cells["A4"].Value = htmlTable;

    package.Save();
    package.Dispose();

}
Posted
Updated 6-Jan-22 3:11am

1 solution

You can't just insert HTML content into a cell and expect Excel to render it, Excel doesn't natively support HTML in it's cells. Even using the IsRichText doesn't work because it expects RTF-formatted content.

How can I embed HTML file in excel? - Stack Overflow[^]

Some suggestions include instead embedding a browser viewport within a cell and then rendering it there. I'm not sure if this works, you might need to search around.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900