1

我的任务是使用 OPEN XML SDK 2.0 并且遇到了这个问题。单元格内的单个 CellValue 是否可以有不同的样式,如下图所示:

在此处输入图像描述

A:纯文本

B:粗体和下划线

注意:我只需要在一个单元格中,谢谢:)

4

1 回答 1

3

是的,这是可能的。一种方法是格式化将插入到SharedStringTable. 此代码段将在上面创建您的示例:

        // 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;
        }

您可以将其插入,SharedStringTable然后将单元格值设置SharedStringTable为插入位置的索引。

可能还有一些我忘记包含的其他参考资料,这些参考资料可能在StylesPart. 我建议在空白 Excel 文档中创建此示例,然后使用Open XML Productivity Tool查看 XML。该工具还将为您提供我在上面为您提供的代码。它应该为您提供下一步的大致方向。

于 2012-01-24T11:34:58.583 回答