How do I get partial cell styling in excel using EPpplus?

风格不统一 提交于 2019-12-08 08:42:58

问题


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.
            }
        }

回答1:


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

 }


来源:https://stackoverflow.com/questions/21884086/how-do-i-get-partial-cell-styling-in-excel-using-eppplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!