Home » excel » c# – Two CellValues with Different Styles in a cell in OPEN XML SDK 2.0

c# – Two CellValues with Different Styles in a cell in OPEN XML SDK 2.0

Posted by: admin May 14, 2020 Leave a comment

Questions:

Im tasked to use OPEN XML SDK 2.0 and had encountered this problem. Is it possible to have different styles for a single CellValue inside a cell something like the picture below:

enter image description here

A: The plain text

B: Bold and Underlined

NOTE: I need both in a single cell only thanks 🙂

How to&Answers:

Yes that is possible. One way is to format the value that will be inserted into the SharedStringTable. This snippet will create your example above:

        // Creates an SharedStringItem instance and adds its children.
        public SharedStringItem GenerateSharedStringItem()
        {
            SharedStringItem sharedStringItem1 = new SharedStringItem();

            Run run1 = new Run();

            RunProperties runProperties1 = new RunProperties();
            Bold bold1 = new Bold();
            Underline underline1 = new Underline();
            FontSize fontSize1 = new FontSize(){ Val = 11D };
            Color color1 = new Color(){ Theme = (UInt32Value)1U };
            RunFont runFont1 = new RunFont(){ Val = "Calibri" };
            FontFamily fontFamily1 = new FontFamily(){ Val = 2 };
            FontScheme fontScheme1 = new FontScheme(){ Val = FontSchemeValues.Minor };

            runProperties1.Append(bold1);
            runProperties1.Append(underline1);
            runProperties1.Append(fontSize1);
            runProperties1.Append(color1);
            runProperties1.Append(runFont1);
            runProperties1.Append(fontFamily1);
            runProperties1.Append(fontScheme1);
            Text text1 = new Text();
            text1.Text = "Project Name:";

            run1.Append(runProperties1);
            run1.Append(text1);

            Run run2 = new Run();

            RunProperties runProperties2 = new RunProperties();
            FontSize fontSize2 = new FontSize(){ Val = 11D };
            Color color2 = new Color(){ Theme = (UInt32Value)1U };
            RunFont runFont2 = new RunFont(){ Val = "Calibri" };
            FontFamily fontFamily2 = new FontFamily(){ Val = 2 };
            FontScheme fontScheme2 = new FontScheme(){ Val = FontSchemeValues.Minor };

            runProperties2.Append(fontSize2);
            runProperties2.Append(color2);
            runProperties2.Append(runFont2);
            runProperties2.Append(fontFamily2);
            runProperties2.Append(fontScheme2);
            Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
            text2.Text = " ALLAN";

            run2.Append(runProperties2);
            run2.Append(text2);

            sharedStringItem1.Append(run1);
            sharedStringItem1.Append(run2);
            return sharedStringItem1;
        }

You can insert that into the SharedStringTable and then set the cell value to be the index in the SharedStringTable where this was inserted.

There might be some other references that I forgot to include that might be defined in the StylesPart. I recommend creating this example in a blank Excel document and then using the Open XML Productivity Tool to look at the XML. The tool will also supply you with the code I provided you above. It should give you a general direction on where to go next.