Search code examples
c#excelepplus

How to check bold style in wrapText in excel file using c#


How to check bold text inside cells of Excel sheet? I'm using C#, Epplus for reading excel file, but I don't resolve how to resolve my task.So can you please tell me how ti resolved it?

Input: In cell of excel

  •   On Command 
  •   **On proceeds**
  •   Exclude guidance
  •   **On Demand**

Output:

•  **On proceeds**
•   **On Demand**

Solution

  • Not sure what you're really after, but assuming you have a spreadsheet like this:

    enter image description here

    Where bold text is in red, this extracts all cells with bold text:

    using (var package = new ExcelPackage(new FileInfo(path)))
    {
        var sheet = package.Workbook.Worksheets[1];
        for (int i = 1; i <= 4; ++i)
        {
            var cell = sheet.Cells[1, i];
            if (cell.IsRichText) {
                foreach (var element in cell.RichText)
                {
                    if (element.Bold) 
                        Console.WriteLine("Rich Text cell {0}: bold text: [{1}]", i, element.Text.Trim());
                }
            }
            else {
                if (cell.Style.Font.Bold)
                    Console.WriteLine("Single-line cell {0}: bold text: [{1}]", i, cell.Value);
            }
        }
    }
    

    Output:

    Single-line cell 1: bold text: [Bold]
    Rich Text cell 3: bold text: [Bold]
    Rich Text cell 4: bold text: [Bold00]
    Rich Text cell 4: bold text: [Bold01]