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);
}
Run Code Online (Sandbox Code Playgroud)
我想更改表格单元格中的字体大小。你能帮我解决这个问题吗?我不知道为什么他们没有为单元格字体大小添加属性。
要更改表格单元格的字体大小,您需要RunProperties在 Run. 字体大小FontSize在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);
}
Run Code Online (Sandbox Code Playgroud)