OPEN XML SDK 2.0 中单元格中具有不同样式的两个 CellValue

发布于 2024-12-28 14:50:50 字数 210 浏览 1 评论 0原文

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

在此处输入图像描述

A:纯文本

B:粗体和下划线

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

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2025-01-04 14:50:50

是的,这是可能的。一种方法是格式化将插入到 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用于查看 XML 的工具。该工具还将向您提供我上面提供给您的代码。它应该为您提供下一步的总体方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文