Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Dec 22, 2016 10:25 am

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)
{

}

}

sarafrank.ssf@gmail.com
 
Posts: 12
Joined: Wed Dec 09, 2015 9:32 am

Fri Dec 23, 2016 6:07 am

Hi,

Thanks for your inquiry.
I have tested the scenario with the latest Spire.XLS Pack Hotfix Version:7.11.13, but didn't find any issue, only the specified range of characters is formatted. Please try to use this version.
If the issue still exists, please provide us more information and a sample project to help us investigate further.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Dec 27, 2016 6:21 am

Hi,

How is the issue now ?
Could you please give us some feedback at your convenience ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS