Open XML SDK:格式化 Excel 单元格的一部分

发布于 2024-12-02 11:09:45 字数 443 浏览 0 评论 0原文

将 Open XML for Excel 与 DocumentFormat.OpenXml.Spreadsheet 结合使用,如何仅将部分文本设置为粗体?

var cell = new Cell {
    //DataType = CellValues.InlineString,
    CellReference = "A" + 1
};

// TODO: Set "bold text" to bold style
//var inlineString = new InlineString();
//inlineString.AppendChild(new Text { Text = "Normal text... bold text..." });
//cell.AppendChild(inlineString);

注释掉现在使用的代码,但应该或也许应该更改。

Using Open XML for Excel with DocumentFormat.OpenXml.Spreadsheet, how do I set only part of a text to bold?

var cell = new Cell {
    //DataType = CellValues.InlineString,
    CellReference = "A" + 1
};

// TODO: Set "bold text" to bold style
//var inlineString = new InlineString();
//inlineString.AppendChild(new Text { Text = "Normal text... bold text..." });
//cell.AppendChild(inlineString);

Commented out code that's used now, but should or maybe should be changed.

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

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

发布评论

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

评论(1

杀手六號 2024-12-09 11:09:45

要仅将部分文本设置为粗体,您需要通过将文本插入 SharedStringTable 并将单元格的数据类型设置为 SharedString 而不是 InlineString 来控制它。这将使 CellValue 成为该表的引用,如 0、1、2 等,并允许比执行内联字符串进行更多控制。

以下是一些示例代码,介绍如何将短语“普通文本...粗体文本...”的第二部分设置为粗体:

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

            Run run1 = new Run();
            Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
            text1.Text = "Normal text… ";

            run1.Append(text1);

            Run run2 = new Run();

            RunProperties runProperties1 = new RunProperties();
            Bold bold1 = new Bold();
            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(fontSize1);
            runProperties1.Append(color1);
            runProperties1.Append(runFont1);
            runProperties1.Append(fontFamily1);
            runProperties1.Append(fontScheme1);
            Text text2 = new Text();
            text2.Text = "bold text…";

            run2.Append(runProperties1);
            run2.Append(text2);

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

要使用此方法,您需要首先找到 SharedStringTable 的实例然后将新的 ShareStringItem 插入其中:

            using (MemoryStream stream = new MemoryStream())
            {
                // create in-memory copy of the Excel template file
                byte[] byteArray = File.ReadAllBytes(TEMPLATE_FILE_NAME);
                stream.Write(byteArray, 0, (int)byteArray.Length);

                using (SpreadsheetDocument document = SpreadsheetDocument.Open(stream, true))
                {
                    // Set private variable template component references (for reuse between methods)
                    mExcelWorkbookPart = document.WorkbookPart;
                    mSharedStringTablePart = mExcelWorkbookPart.SharedStringTablePart;

                    mSharedStringTablePart.SharedStringTable.AppendChild(GenerateSharedStringItem());
                }

                return stream.ToArray();
            }

To only set part of the text to bold you will want to control that by inserting your text into the SharedStringTable and making your cell's data type be SharedString and not InlineString. This will make the CellValue be a reference into this table, like 0, 1, 2, etc. and allow more control then doing an inline string.

Here is some sample code on how to make the second part of the pharse "Normal text... bold text..." bold:

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

            Run run1 = new Run();
            Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
            text1.Text = "Normal text… ";

            run1.Append(text1);

            Run run2 = new Run();

            RunProperties runProperties1 = new RunProperties();
            Bold bold1 = new Bold();
            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(fontSize1);
            runProperties1.Append(color1);
            runProperties1.Append(runFont1);
            runProperties1.Append(fontFamily1);
            runProperties1.Append(fontScheme1);
            Text text2 = new Text();
            text2.Text = "bold text…";

            run2.Append(runProperties1);
            run2.Append(text2);

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

To use this method you want to first find an instance of the SharedStringTable and then insert your new ShareStringItem into it:

            using (MemoryStream stream = new MemoryStream())
            {
                // create in-memory copy of the Excel template file
                byte[] byteArray = File.ReadAllBytes(TEMPLATE_FILE_NAME);
                stream.Write(byteArray, 0, (int)byteArray.Length);

                using (SpreadsheetDocument document = SpreadsheetDocument.Open(stream, true))
                {
                    // Set private variable template component references (for reuse between methods)
                    mExcelWorkbookPart = document.WorkbookPart;
                    mSharedStringTablePart = mExcelWorkbookPart.SharedStringTablePart;

                    mSharedStringTablePart.SharedStringTable.AppendChild(GenerateSharedStringItem());
                }

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