Hi,
I wrote a class in order to add format to rich text from html.
the problem is, when I add format, it adds for all the cell instead of few places.
public static void GetHtmlCode(ref CellRange range, string html, Workbook workbook)
{
try
{
List<FontDetails> fontList = new List<FontDetails>();
RichTextExcelFormat excelFormat = new RichTextExcelFormat();
excelFormat.GetHtmlFormat(html);
foreach (HtmlTextDetails item in excelFormat.allHtml)
{
if (item.text != "")
{
int ind = range.RichText.Text.Length;
range.RichText.Text = range.RichText.Text + item.text.Trim();
ExcelFont excelFont = workbook.CreateFont();
excelFont.FontName = "Times New Roman";
TagDetail b = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.B select f).FirstOrDefault();
if (b != null)
{
excelFont.IsBold = true;
}
else
excelFont.IsBold = false;
TagDetail u = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.U select f).FirstOrDefault();
if (u != null)
{
excelFont.Underline = FontUnderlineType.Single;
}
else
excelFont.Underline = FontUnderlineType.None;
TagDetail i = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.I select f).FirstOrDefault();
if (i != null)
{
excelFont.IsItalic = true;
}
else
excelFont.IsItalic = false;
TagDetail sup = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.SUP select f).FirstOrDefault();
if (sup != null)
{
excelFont.IsSuperscript = true;
}
else
excelFont.IsSuperscript = false;
TagDetail sub = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.SUB select f).FirstOrDefault();
if (sub != null)
{
excelFont.IsSubscript = true;
}
else
excelFont.IsSubscript = false;
TagDetail font = (from f in item.tagTypes where f.tagType == RichTextExcelFormat.FONT select f).FirstOrDefault();
if (font != null)
{
excelFont.Color = System.Drawing.ColorTranslator.FromHtml(font.value);
}
else
excelFont.Color = Color.Black;
FontDetails fontD = new FontDetails();
fontD.start = ind;
fontD.end = range.RichText.Text.Length;
fontD.font = excelFont;
fontList.Add(fontD);
}
}
foreach (FontDetails f in fontList)
{
try
{
if (f.start != f.end)
{
if (f.end > range.RichText.Text.Length - 1)
range.RichText.SetFont(f.start, range.RichText.Text.Length - 1, f.font);
else
range.RichText.SetFont(f.start, f.end, f.font);
}
}
catch (Exception ee)
{
}
}
//range.AutoFitRows();
//range.Style.WrapText = true;
range.IsWrapText = true;
}
catch (Exception ex)
{
}
}