Excel text color> 255 characters

I wrote a third-party sw plugin that extracts revised texts to an Excel worksheet and then paints the changed parts in Excel. This works as long as each text segment (= cell content) does not exceed 255 characters. Alas, this can and does happen sometimes.

To identify changed parts in Excel, I surround them with <del>resp. <add>tags for deleted and added texts. Then I color these parts (and remove the surrounding tags) as follows:

while (((string)cell1.Text).Contains("<del>"))
{
    try
    {
        var pos = ((string) cell1.Text).IndexOf("<del>") + 1;
        var pos2 = ((string) cell1.Text).IndexOf("</del>") + 1;
        var txt = cell1.Characters[pos, (pos2-pos) + 9].Text;

        txt = txt.Replace("<del>", "").Replace("</del>", "");
        cell1.Characters[pos, (pos2-pos) + 9].Text = txt;
        cell1.Characters[pos, txt.Length-3].Font.Color = -16776961;
    }
    catch
    {
            break;
    }
}

I use Interop because it is much more convenient for me to work, and also because I could not find a worthy example of how to do this with OpenXML. However, I know that Excel has its limits when it comes to cell text, so I am open to suggestions.

, > 255 , Interop?

, , , Word , , / Excel (yukk). , .

P.S: , Excel.

+4
1

, OpenXML. , , , , , , .

var xlsx = SpreadsheetDocument.Open(xlsPath, true);
var contents = xlsx.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
foreach (SharedStringItem si in contents.SharedStringTable.Elements<SharedStringItem>())
{
    if (si.Text != null)
    {
        XlHelper.ColorCellText(si, "del", new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = "FFFF0000" });
        XlHelper.ColorCellText(si, "add", new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = "0000BF00" });
    }
}

XlHelper.ColorCellText:

public static void ColorCellText(SharedStringItem si, string TagName, DocumentFormat.OpenXml.Spreadsheet.Color col)
{
    var newText = si.InnerText;
    var startTag = string.Format("<{0}>", TagName);
    var endTag = string.Format("</{0}>", TagName);
    if (newText.Contains(startTag))
    {
        si.Text.Remove();
        var lastpos = 0;
        while (newText.Contains(startTag))
        {
            try
            {
                var pos1 = newText.IndexOf(startTag);
                var pos2 = newText.IndexOf(endTag);
                var txtLen = pos2 - pos1 - 5;
                var it = string.Concat(newText.Substring(0, pos1), newText.Substring(pos1 + 5, txtLen),
                    newText.Substring(pos2 + 6));

                var run = new Run();
                var txt = new Text
                {
                    Text = it.Substring(0, pos1),
                    Space = SpaceProcessingModeValues.Preserve
                };
                run.Append(txt);
                si.Append(run);

                run = new Run();
                txt = new Text
                {
                    Text = it.Substring(pos1, txtLen),
                    Space = SpaceProcessingModeValues.Preserve
                };

                var rp = new RunProperties();

                rp.Append(col.CloneNode(true));
                run.RunProperties = rp;
                run.Append(txt.CloneNode(true));
                si.Append(run.CloneNode(true));

                newText = newText.Substring(pos2 + 6);
            }
            catch(Exception ex)
            {
                using (var sw = new StreamWriter(logFile, true))
                {
                    sw.WriteLine("Error: {0}\r\n{1}", ex.Message, newText);
                }
                break;
            }
        }
        if (newText.Length>=0)
        {
            var lastrun = new Run();
            var lasttxt = new Text
            {
                Text = newText,
                Space = SpaceProcessingModeValues.Preserve
            };
            lastrun.Append(lasttxt);
            si.Append(lastrun);
        }
    }
}

Space = SpaceProcessingModeValues.Preserve, , .

, EPPlus, In-Cell Richtext .

0

Source: https://habr.com/ru/post/1651689/


All Articles