How can I read partially bold text from a cell

If a cell A1 has the data of “Test Data Set” but only the Data is in bold. How can I read the bold text in A1 which should return only Data.

I tried the following but it doesn’t work. It only returns text if the cell is entirely in bold such as “Test Data Set”.

Cell cell = worksheet.Cells[“A1”];
string bold = “”;
for (int c = 0; c < cell.Value.ToString().Length; c++)
{
if (cell.Characters(c,1).Font.IsBold)
{
bold += cell.Value.ToString().Substring(c,1);
}
}

Thanks.

Hi,

When you want to check the richly formatted data in a cell, it is not so straight.

Please modify your code a bit accordingly, see the complete sample code for your reference, hopefully it will help you. I have tested with a simple template file and it works.

Workbook workbook = new Workbook();
//Open an excel file.
//The file contains richly formatted data i.e. “Test Data Set”
// in the A1 cell of the first worksheet.
workbook.Open(@“e:\test\richBook1.xls”);
Worksheet worksheet = workbook.Worksheets[0];
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;

ArrayList chars = cells[“A1”].GetCharacters();
int myCharCount = chars.Count;
//It depends on the rich text formatting a cell can have
//For “Test Data Set” it will have 3.
MessageBox.Show(myCharCount.ToString());

Aspose.Cells.Cell cell = worksheet.Cells[“A1”];
string bold = “”;
for (int c = 0; c < cell.Value.ToString().Length; c++)
{
bool isbold = IsBold(c, 1, chars);
if (isbold)
{
bold += cell.Value.ToString().Substring(c, 1);

}
}

MessageBox.Show(bold); //It will give “Data”.
}

//Method to check whether the characters are bold in the given string.
internal static bool IsBold(int startIndex, int length, ArrayList chars)
{
for (int i = 0; i < chars.Count; i++)
{
Characters chs = (Characters)chars[i];
if (chs.StartIndex >= startIndex)
{
if (chs.StartIndex < startIndex + length)
{
if (!chs.Font.IsBold)
{
return false;
}
}
}
else if (chs.StartIndex + chs.Length > startIndex)
{
if (!chs.Font.IsBold)
{
return false;
}
}

}
return true;
}


Also, I have attached the latest fix/version, please try it. The template file is also attached.

Thank you.