Here's a function we wrote to convert a Cell with RichText or a hyperlink to HTML:
public static string RichTextToHTML(IXLCell cell)
{
StringBuilder myString = new StringBuilder();
// Special case when the whole cell has a link vs. a http:// string in one of the rich text parts below in the loop
if (cell.HasHyperlink && cell.Hyperlink.IsExternal) myString.Append("<a href='" + cell.Hyperlink.ExternalAddress + "'>");
foreach (var richText in cell.RichText)
{
myString.Append("<span style='"); // Start SPAN and CSS here then add to it below
if (richText.Bold) myString.Append("font-weight: bold;");
if (richText.Strikethrough) myString.Append("text-decoration:line-through;");
if (richText.Italic) myString.Append("font-style: italic;");
if (richText.Underline == XLFontUnderlineValues.Single) myString.Append("text-decoration: underline;");
if (richText.FontName != "Arial") myString.Append("font-face: " + richText.FontName + ";");
if (richText.FontColor.Color.IsKnownColor) myString.Append("color: " + richText.FontColor.Color.Name + ";");
if (richText.FontSize != 10) myString.Append("font-size: " + richText.FontSize + ";");
myString.Append("'>"); // End the SPAN
if (richText.Text.Contains("http")) myString.Append("<a href='" + richText.Text + "'>");
myString.Append(richText.Text.Replace("\r\n", "<br>"));
if (richText.Text.Contains("http")) myString.Append("</a>");
myString.Append("</span>");
}
if (cell.HasHyperlink && cell.Hyperlink.IsExternal) myString.Append("</a>");
return myString.ToString();
}
↧