c# - 如何使用 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 存在于所有单元格中,包括空单元格。
如何在不显示任何文本的情况下设置单元格的字体大小?
解决方案
我确认你的报告。解决它的一种方法是用空格" "
代替“空”字符串,当然,将“空格保留”添加到文本运行中。
另一种可能性是创建字符样式并将其应用于表格单元格。事实证明这比听起来更棘手,因为样式需要两次应用于包含文本的单元格:一次应用于 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;
}
推荐阅读
- python-3.6 - python中类的行为
- java - SWT.graphics.color 的 Alpha 值不起作用
- scala - 光滑的导入钻石运算符 <>
- apache-camel - 如何在 Camel JMS 选择器选项中使用加号?
- r - 具有多列和权重列的桑基图 - 使用 NetworkD3 包
- image - macos Mojave beta - 图像缩略图问题
- android - React Native 生成签名的 APK - :app:transformClassesWithJarMergingForRelease
- sql-server - 如何获取proc中每个select语句的行数
- node.js - 如何使用代理修复错误?
- spring-data - 没有映射器注释的弹簧数据dynamodb