使用Word和C#更改单个单元格中单个单词的字体 [英] Change font of single word in single cell using Word and C#

查看:251
本文介绍了使用Word和C#更改单个单元格中单个单词的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用C#创建了一个包含表的Word文档.现在,我需要更改单元格一部分的字体.无论我在网上搜索到什么,都会影响整个单元格,并归结为:

Hi,

I created a Word document containing a table, using C#. Now I need to change the font of a part of the cell. Whatever I found searching the web always affected the whole cell and boils down to:

string line1 = ...
string line2 = ...

Word.Range rng = tbl.Cell(intRow, 2).Range;
if (line2 == string.Empty {
  rng.Font.Size = 10;
  rng.Text = line1; }
else {
  rng.Text = line1 + Environment.NewLine + line2;
  rng.Font.Size = 8;
  rng.SetRange(0, line1.Length);
  rng.Font.Size = 10; }



这会将完整单元格设置为fontsize 8,而不仅仅是单元格中的第二行.



This sets the complete cell to fontsize 8, instead of just the second line in the cell.

Does anyone have a solution for this?

推荐答案

您要更改已设置为单元格的rng变量的字体,因此当然可以更改整个单元格的字体.您需要选择所需的文本,而不是整个单元格.
You are changing the font for your rng variable, which has been set to the cell, so of course it will change the font for the entire cell. You need to select the text you want and not the entire cell.


沃尔特,

你是对的.观看了很多小时之后,您根本看不到明显的...
更改为:
Hi Walt,

you''re right. After looking at it for so many hours you simply don''t see the obvious...
Changed it to:
string line1 = ...
string line2 = ...
Word.Range rng = tbl.Cell(intRow, 2).Range;
if (line2 == string.Empty {
  rng.Font.Size = 10;
  rng.Text = line1; }
else {
  rng.Text = line1 + Environment.NewLine + line2;
  rng.Font.Size = 8;
  //rng.SetRange(0, line1.Length);
  rng = tbl.Cell(intRow, 2).Range.Paragraphs.Items(1).Range;
  rng.Font.Size = 10; }


正是我想要的.

谢谢!

DutchJohn


which did exactly what I wanted it to do.

Thanks!

DutchJohn


这篇关于使用Word和C#更改单个单元格中单个单词的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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