Open XML change fontsize of table

做~自己de王妃 提交于 2019-12-11 02:49:50

问题


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);
}

I want to change fontsize in table cell. Can you help me with that? I don't know why they didn't add a property for cell fontsize.


回答1:


To change the fontsize of a table cell, you need to add a RunProperties to the Run. The fontsize is specified inside a FontSize element inside that RunProperties.

For example to change all of your entries to fontsize 18, your code would look like:

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);
}


来源:https://stackoverflow.com/questions/44692925/open-xml-change-fontsize-of-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!