想要图里的效果,选中一段文字,然后给个文字一个背景颜色,用NPOI2.4.1 如何设置?
参考调用spire.doc.dll Version7.11的方法来设置表格文字背景色,这里用免费版的就可以实现效果:
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetTextBackgroundInCell
{
class Program
{
static void Main(string[] args)
{
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取Section
Section section = doc.Sections[0];
//获取表格
Table table = section.Tables[0] as Table;
//获取表格中的指定单元格
TableCell cell = table[1, 2];
//查找单元格内的指定文本内容,并设置文本背景色
TextSelection text = cell.Paragraphs[0].Find("色拉油", true, false);
text.GetAsOneRange().CharacterFormat.TextBackgroundColor = Color.Red;
//保存文档
doc.SaveToFile("TextBackground.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("TextBackground.docx");
}
}
}
测试效果: