在表OpenXML C#中设置字体格式 [英] Format Font Inside Table OpenXML C#

查看:111
本文介绍了在表OpenXML C#中设置字体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenXML WordProcessing制作表格.我想格式化单元格内的字体.这是我的代码

I want to make table using OpenXML WordProcessing. I want to format the font inside the cell. This is my code

MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Body body = mainDocumentPart.Document.AppendChild(new Body());

RunProperties runHeader = new RunProperties();
RunFonts runFont = new RunFonts();
runFont.Ascii = "Lucida Sans";
runHeader.Append(runFont);                    
runHeader.Append(new Bold());
runHeader.Append(new FontSize() { Val = "16" }); 

//// Create a new table
Table tbl = new Table();

tr = new TableRow();
tc = new TableCell();

Paragraph paraHeader = new Paragraph();
Text heading_text = new Text("Company Name");
runHeader.Append(heading_text);
paraHeader.Append(runHeader);
tc.Append(paraHeader);
tr.Append(tc);
tbl.Append(tr);

body.AppendChild(tbl);

但是当我打开Microsoft Word时,出现了错误.它说该文件的内容有问题

But when I open up on Microsoft Word, I got error. Its said that the file has problem with the contents

推荐答案

您要将文本追加到运行属性",需要将其追加到运行". 试试:

You are appending your text to your Run Properties, it needs to be appended to a Run. try:

Text heading_text = new Text("Company Name");

////create the run
Run runHeaderRun = new Run();

////append the run properties and text to the run
runHeaderRun.Append(runHeader);
runHeaderRun.Append(heading_text);

////append the run to the paragraph
paraHeader.Append(runHeaderRun);

tc.Append(paraHeader);

这篇关于在表OpenXML C#中设置字体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆