猿问

如何使用 Open XML SDK 在 Word 表格的空单元格中设置字体大小?

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


这是我的示例代码,其中一行带有一个单元格,字体大小应为 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 存在于所有单元格中,包括空单元格。

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


慕田峪7331174
浏览 417回答 1
1回答

胡子哥哥

我确认你报告的内容。一种解决方法是用空格" "代替“空”字符串,当然,在文本运行中添加“空格保留”。另一种可能性是创建字符样式并将其应用于表格单元格。事实证明,这比听起来要棘手,因为样式需要两次应用于包含文本的单元格:一次应用于 ParagraphMarkRunProperties,一次应用于 RunProperties。对于空单元格,样式只需要应用于 ParagraphMarkRunProperties。实际上,经过反思,您可以对字体大小使用相同的方法......我在下面的代码中包含了这两种方法。仅用于字体大小的注释被注释掉(四行)。示例代码假定单行四列表格的第三个单元格没有内容。只有在有内容时才添加运行和文本信息。private void btnCreateTable_Click(object sender, EventArgs e){&nbsp; &nbsp;&nbsp; &nbsp; string filePath = @"C:\X\TestCreateTAble.docx";&nbsp; &nbsp; using (WordprocessingDocument pkg = WordprocessingDocument.Open(filePath, true))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MainDocumentPart partDoc = pkg.MainDocumentPart;&nbsp; &nbsp; &nbsp; &nbsp; Document doc = partDoc.Document;&nbsp; &nbsp; &nbsp; &nbsp; StyleDefinitionsPart stylDefPart = partDoc.StyleDefinitionsPart;&nbsp; &nbsp; &nbsp; &nbsp; Styles styls = stylDefPart.Styles;&nbsp; &nbsp; &nbsp; &nbsp; Style styl = CreateTableCharacterStyle();&nbsp; &nbsp; &nbsp; &nbsp; stylDefPart.Styles.AppendChild(styl);&nbsp; &nbsp; &nbsp; &nbsp; Table t = new Table();&nbsp; &nbsp; &nbsp; &nbsp; TableRow tr = new TableRow();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= 4; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableCell tc = new TableCell(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "500" }));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paragraph para = new Paragraph();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ParagraphProperties paraProps = new ParagraphProperties();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ParagraphMarkRunProperties paraRunProps = new ParagraphMarkRunProperties();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RunStyle runStyl = new RunStyle() { Val = "Table9Point" };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paraRunProps.Append(runStyl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; FontSize runFont = new FontSize() {Val = "18" };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; paraRunProps.Append(runFont);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paraProps.Append(paraRunProps);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; para.Append(paraProps);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Run run = new Run();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text txt = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt = new Text("txt");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt.Space = SpaceProcessingModeValues.Preserve;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RunProperties runProps = new RunProperties();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RunStyle inRunStyl = (RunStyle) runStyl.Clone();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; runProps.Append(inRunStyl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; FontSize inRunFont = (FontSize) runFont.Clone();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; runProps.Append(inRunFont);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run.Append(runProps);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run.Append(txt);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; para.Append(run);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tc.Append(para);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr.Append(tc);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; t.Append(tr);&nbsp; &nbsp; &nbsp; &nbsp; //Insert at end of document&nbsp; &nbsp; &nbsp; &nbsp; SectionProperties sectProps = doc.Body.Elements<SectionProperties>().LastOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; doc.Body.InsertBefore(t, sectProps);&nbsp; &nbsp; }}private Style CreateTableCharacterStyle(){&nbsp; &nbsp; Style styl = new Style()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CustomStyle = true,&nbsp; &nbsp; &nbsp; &nbsp; StyleId = "Table9Point",&nbsp; &nbsp; &nbsp; &nbsp; Type = StyleValues.Character,&nbsp; &nbsp; };&nbsp; &nbsp; StyleName stylName = new StyleName() { Val = "Table9Point" };&nbsp; &nbsp; styl.AppendChild(stylName);&nbsp; &nbsp; StyleRunProperties stylRunProps = new StyleRunProperties();&nbsp; &nbsp; stylRunProps.FontSize = new FontSize() { Val = "18" };&nbsp; &nbsp; styl.AppendChild(stylRunProps);&nbsp; &nbsp; BasedOn basedOn1 = new BasedOn() { Val = "DefaultParagraphFont" };&nbsp; &nbsp; styl.AppendChild(basedOn1);&nbsp; &nbsp; return styl;}
随时随地看视频慕课网APP
我要回答