c# - 打开 XML 更改表格的字体大小

标签 c# openxml-sdk

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/

相关文章:

c# - 在使用 block 或使用 block 声明中声明 IDisposable 成员的区别?

c# - 如何使用 Roslyn 验证特定 Actor 阵容是否有效

c# - 如何使用 MS Open XML SDK 检索一些图像数据和格式?

c# - OpenXML在word文档C#中插入评论回复

openxml - 链接到 Microsoft Open XML SDK 教程

c# - Linq to SQL 分组子关系

c# - 在 View 的引擎 Razor 中将参数作为模型传递

c# - 读取 Excel 文件 (.xls/.xlsx) 的最佳方式

c# - 将新工作表插入电子表格文档 OpenXml

c# - Linq-To-SQL CreateDatabase 不创建存储过程