for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
tr.Append(tc);
}
table.Append(tr);
}
我想更改表格单元格中的字体大小。你能帮我吗?我不知道他们为什么不为单元格字体大小添加属性。
最佳答案
要更改表格单元格的字体大小,您需要将 RunProperties
添加到 Run
。字体大小在 RunProperties 内的 FontSize
元素内指定。 .
例如,要将所有条目更改为字体大小 18,您的代码如下所示:
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var run = new DocumentFormat.OpenXml.Wordprocessing.Run();
var text = new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]);
// your old code for reference: tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
RunProperties runProperties1 = new RunProperties();
FontSize fontSize1 = new FontSize(){ Val = "36" };
runProperties1.Append(fontSize1);
run.Append(runProperties1);
run.Append(text);
paragraph.Append(run);
tc.Append(paragraph);
tr.Append(tc);
}
table.Append(tr);
}
关于c# - 打开 XML 更改表格的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692925/