【问题标题】:How to set font size in empty cell of a Word table using the Open XML SDK?如何使用 Open XML SDK 在 Word 表格的空单元格中设置字体大小?
【发布时间】:2018-07-26 10:05:19
【问题描述】:

我在一个 Word 文件中使用 C# 在 OpenXml 中创建一个表。我使用this 问题中提到的一些代码来设置单元格中文本的字体大小。它适用于包含文本的单元格,但空单元格似乎被赋予了正常样式,并且字体更大,这使得行高更大。

这是我的示例代码,其中一行包含一个单元格,其中字体大小应为 9:

TableRow tr = new TableRow();
TableCell tc = new TableCell();
Paragraph par = new Paragraph();
Run run = new Run();
Text txt = new Text("txt");

RunProperties runProps = new RunProperties();
FontSize fontSize = new Fontsize() { Val = "18" }; // font size 9

runProps.Append(fontSize);

run.Append(runProps);
run.Append(txt);

para.Append(run);
tc.Append(para);
tr.Append(tc);

以下是结果表的示例。如您所见,中间一排比其他排高。在显示“txt”的单元格中,字体大小为 9,但在空白单元格中,字体大小为 11。上面的代码用于所有单元格,其中空单元格仅包含文本“”。当我使用 Open XML Tool 查看该文件时,我可以看到值为 18 的 RunProperties 存在于所有单元格中,包括空单元格。

如何在不显示任何文字的情况下设置单元格的字体大小?

【问题讨论】:

    标签: c# ms-word openxml openxml-sdk


    【解决方案1】:

    我确认您报告的内容。一种解决方法是将空格 " " 替换为“空”字符串,当然,将“空格保留”添加到文本运行中。

    另一种可能性是创建字符样式并将其应用于表格单元格。事实证明这比听起来更棘手,因为样式需要两次 应用于包含文本的单元格:一次应用于 ParagraphMarkRunProperties,一次应用于 RunProperties。对于空单元格,样式只需应用于 ParagraphMarkRunProperties。

    实际上,经过反思,您可以对字体大小使用相同的方法...

    我在下面的代码中包含了这两种方法。仅用于字体大小的一个被注释掉(四行)。

    示例代码假定一行四列表的第三个单元格没有内容。只有有内容时才会添加运行和文本信息。

    private void btnCreateTable_Click(object sender, EventArgs e)
    {   
        string filePath = @"C:\X\TestCreateTAble.docx";
        using (WordprocessingDocument pkg = WordprocessingDocument.Open(filePath, true))
        {
            MainDocumentPart partDoc = pkg.MainDocumentPart;
            Document doc = partDoc.Document;
    
            StyleDefinitionsPart stylDefPart = partDoc.StyleDefinitionsPart;
            Styles styls = stylDefPart.Styles;
            Style styl = CreateTableCharacterStyle();
            stylDefPart.Styles.AppendChild(styl);
    
            Table t = new Table();
            TableRow tr = new TableRow();
    
            for (int i = 1; i <= 4; i++)
            {
                TableCell tc = new TableCell(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "500" }));
                Paragraph para = new Paragraph();
                ParagraphProperties paraProps = new ParagraphProperties();
                ParagraphMarkRunProperties paraRunProps = new ParagraphMarkRunProperties();
                RunStyle runStyl = new RunStyle() { Val = "Table9Point" };
                paraRunProps.Append(runStyl);
                //    FontSize runFont = new FontSize() {Val = "18" };
                //    paraRunProps.Append(runFont);
                paraProps.Append(paraRunProps);
                para.Append(paraProps);
    
                Run run = new Run();
    
                Text txt = null;
                if (i == 3)
                {
                }
                else
                {
                    txt = new Text("txt");
                    txt.Space = SpaceProcessingModeValues.Preserve;
                    RunProperties runProps = new RunProperties();
                    RunStyle inRunStyl = (RunStyle) runStyl.Clone();
                    runProps.Append(inRunStyl);
                    //    FontSize inRunFont = (FontSize) runFont.Clone();
                    //    runProps.Append(inRunFont);
                    run.Append(runProps);
                    run.Append(txt);
                    para.Append(run);
               }
                tc.Append(para);
                tr.Append(tc);
            }
            t.Append(tr);
            //Insert at end of document
            SectionProperties sectProps = doc.Body.Elements<SectionProperties>().LastOrDefault();
            doc.Body.InsertBefore(t, sectProps);
        }
    }
    
    private Style CreateTableCharacterStyle()
    {
        Style styl = new Style()
        {
            CustomStyle = true,
            StyleId = "Table9Point",
            Type = StyleValues.Character,
        };
        StyleName stylName = new StyleName() { Val = "Table9Point" };
        styl.AppendChild(stylName);
        StyleRunProperties stylRunProps = new StyleRunProperties();
        stylRunProps.FontSize = new FontSize() { Val = "18" };
        styl.AppendChild(stylRunProps);
        BasedOn basedOn1 = new BasedOn() { Val = "DefaultParagraphFont" };
        styl.AppendChild(basedOn1);
        return styl;
    }
    

    【讨论】:

    • 感谢您的回答!我最终将我想要的字体大小添加到段落运行属性中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多