How to extract the bold text in the cell?

Hi,

We have a spreadsheet with some bold text inside the cell. how can we just extract that bold text through Aspose .cell?

I have attached the sample file.

Example:

Cell Value:

Vip To Go

I only want to extract "Vip".

Thanks.

Laura

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.2.2.1


First you need to call Cell.GetCharacters(), it will return you array of FontSetting, from that you can find how many characters are bold.

In case, if it returns null, then you should check the Cell.GetStyle() i.e Style object.

Please see the full code below and its output.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\NameSample.xlsx”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];


foreach (Cell cell in worksheet.Cells)

{

FontSetting[] fntSettings = cell.GetCharacters();


if (fntSettings != null)

{

foreach (FontSetting fnt in fntSettings)

{


if (fnt.Font.IsBold == true)

{

string boldText = cell.StringValue.Substring(fnt.StartIndex, fnt.Length);


Debug.WriteLine(“[Cell Name]: " + cell.Name + " [Bold Text]: " + boldText);

}//if

}//foreach

}//if



//In case if it is null, then check style object

Style st = cell.GetStyle();


if (fntSettings == null && st.Font.IsBold == true)

{

string boldText = cell.StringValue;


Debug.WriteLine(”[Cell Name]: " + cell.Name + " [Bold Text]: " + boldText);

}



}//foreach



Output:
[Cell Name]: A2 [Bold Text]: Vip
[Cell Name]: A3 [Bold Text]: Wrti
[Cell Name]: A4 [Bold Text]: Hrp
[Cell Name]: A5 [Bold Text]: Hp
[Cell Name]: A6 [Bold Text]: Isqft
[Cell Name]: A7 [Bold Text]: Itc
[Cell Name]: A7 [Bold Text]: Usa
[Cell Name]: A8 [Bold Text]: Spc
[Cell Name]: A9 [Bold Text]: Kbms
[Cell Name]: A9 [Bold Text]: Llc
[Cell Name]: A10 [Bold Text]: Ecc
[Cell Name]: A10 [Bold Text]: Llc
[Cell Name]: A11 [Bold Text]: Mf
[Cell Name]: A12 [Bold Text]: Kl
[Cell Name]: A13 [Bold Text]: Cbm