Question

I have a cell that has some text in italics and some text not in italics. How do I preserve the formatting? I looked at the OfficeOpenXml.Style.ExcelStyle and see the options hanging off for bold, italic, etc but those apply to the whole cell. Is there a way to tell which text is formatted a certain way using eppplus and to preserve that formatting?

    FileInfo info = new FileInfo(@"C:\excel.xlsx");
        using (ExcelPackage package = new ExcelPackage(info)) {
            ExcelWorksheet sheet = package.Workbook.Worksheets["Sheet"];
            object text = sheet.Cells[1, 1].Value;

            // if I do toString, I lose all formatting:
            string textWithOutFormat = text.ToString();
            // I would assume I'd get it some how like this?
            if (sheet.Cells[1, 1].IsRichText) {
                // get rich text formatting or get what items are wrapp in rich text
                // no idea how to get it.
            }
        }
Was it helpful?

Solution

To write a cell with several style :

FileInfo fi = new FileInfo(@"c:\File.xlsx");

using (ExcelPackage package = new ExcelPackage(fi))
{

    ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheets"];

    worksheet.Cells[1, 1].IsRichText = true;
    ExcelRichText richtext = worksheet.Cells[1, 1].RichText.Add("Test Italic");
    richtext .Italic = true;

    richtext = worksheet.Cells[1, 1].RichText.Add("Test 2");
    richtext .Italic = false;

    package.Save();
}

To read a cell with several style :

foreach(var part in cell.RichText)
 {
   if (part.Bold)
    // Do staff

   if (part.Italic)
    // Do staff

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top