如何使用Open XML SDK在Word表的空白单元格中设置字体大小? [英] How to set font size in empty cell of a Word table using the Open XML SDK?

查看:253
本文介绍了如何使用Open XML SDK在Word表的空白单元格中设置字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Word文件中的C#在OpenXml中创建一个表.我使用了问题中提到的一些代码来设置细胞.它适用于包含文本的单元格,但空白单元格似乎具有正常的样式,并且具有较大的字体大小,从而使行高更大.

I am creating a table in OpenXml with C# in a Word file. I used some code mentioned in this question to set the fontsize of the text in the cells. It works fine for the cells that contain text, but the empty cells seem to be given the normal style, and with that a bigger font size, which makes the row height bigger.

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

Here is my sample code with a single row with a single cell, where the font size should be 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的RunProperty.

Here is an example of the resulting table. As you can see the middle row is taller than the others. In the cells that say "txt" the font size is 9, but in the blank cell the font size is 11. The code above is used for all the cells, where the empty cell simply has the text "". When I looked at the file with the Open XML Tool, I can see that the RunProperties with value 18 is there for all the cells including the empty one.

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

推荐答案

我确认您的举报.解决该问题的一种方法是,用空格" "代替空"字符串,然后在文本运行中添加空格保留".

I confirm what you report. One way around it would be to substitute a space " " for the "empty" string, adding "space preserve" to the text run, of course.

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

Another possibility would be to create a character style and apply it to the table cells. This turned out to be trickier than it sounds as the style needs to be applied twice to cells that contain text: once to the ParagraphMarkRunProperties and once to the RunProperties. For empty cells the style need be applied only to the ParagraphMarkRunProperties.

实际上,在反射时,您可以对字体大小使用相同的方法...

Actually, on reflection, you can use the same approach for the font size...

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

I've included both approaches in the code below. The one for just the font size is commented out (four lines).

该示例代码假定单行四列表的第三个单元格没有内容.仅当有内容时,才添加运行和文本信息.

The sample code assumes that the third cell of the one-row, four column table, has no content. Run and Text information is added only when there is content.

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

这篇关于如何使用Open XML SDK在Word表的空白单元格中设置字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆