Click here to Skip to main content
15,867,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do i convert a string in to html format?
To be precise, i want to convert excel cell value in to html format.
For example:
if excel cell values is 'am a programmer'. I want to convert it in to 'am a <<b>programmer<</b>'.
Is it possible?
If any values are bold then html bold tag should come in front of that.If any word is italic then italic tag should come in front of that.
I can read excel values using excel interop.I can check values are bold or not. But if any values are bold in between am unable to check using code.
So i want to convert that excel cell value in to html.

Any way ?

c#,.net
Posted
Updated 1-Dec-14 21:38pm
v3
Comments
Sinisa Hajnal 1-Dec-14 2:18am    
Only single cell or the whole sheet? There is Save As...html format.
Am Gayathri 1-Dec-14 2:34am    
Could you please explain that ?
Sinisa Hajnal 1-Dec-14 3:09am    
What I meant is, are you trying to open the excel table in .NET and to save as HTML only parts (cells) of the sheet or you simply need to convert whole excel to web page.
Am Gayathri 1-Dec-14 3:15am    
no what i want is,
i want to show excel values in grid with same format.Ie if excel values are bold i wanna show it as bold in grid.
I i enter 'Test' this come as bold in grid so i want html format of that value in excel.
See this question http://www.codeproject.com/Questions/845070/How-to-check-whether-excel-values-are-formatted-bo?arn=0
Sinisa Hajnal 2-Dec-14 5:17am    
You provided good answer there, why this question? Either have another table which will hold the cell location and format or add new columns to the original table that will hold the formatting for entire row.

1 solution

Loop through each character in excel then check the format.
XML
StringBuilder html = new StringBuilder();
                              for (int index1 = 1; index1 <= MySheet.get_Range("B" + index, "B" + index).Cells.Text.ToString().Length; index1++)
                               {
                                   Microsoft.Office.Interop.Excel.Characters ch = MySheet.get_Range("B" + index, "B" + index).Cells.get_Characters(index1, 1);
                                   bool bold = (bool) ch.Font.Bold;
                                   bool italic = (bool)ch.Font.Italic;

                                   if (bold && italic)
                                   {
                                       html.Append("<b><i>");
                                       html.Append(ch.Text);
                                       html.Append("</b></i>");
                                   }
                                   else if (bold)
                                   {
                                       html.Append("<b>");
                                       html.Append(ch.Text);
                                       html.Append("</b>");
                                   }
                                   else if (italic)
                                   {
                                       html.Append("<i>");
                                       html.Append(ch.Text);
                                       html.Append("</i>");
                                   }
                                   else
                                   {
                                       html.Append(ch.Text);
                                   }
                               }




The final html will be like am a <<b>programmer<</b>
 
Share this answer
 
v2

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