简体   繁体   English

如何使字符串粗体的一部分并将其添加到Excel Cell?

[英]How to make part of a string Bold and add it to a excel Cell?

Hello i try to make a part of a string bold and then add it to an excel Cell. 您好,我尝试将字符串的一部分设为粗体,然后将其添加到Excel Cell中。 So it looks like: 所以看起来像:

Excel单元格

What i tried: 我试过的

Inside Excel using a range: 在Excel中使用范围:

excelSheet.get_Range("A" + 16, "D" + 16).Font.Bold = true;

But this makes everything bold.... 但这使一切大胆....

Then i tried: 然后我尝试了:

"<b>" + text + "<b>"

and had no success. 并没有成功。

So i make something wrong. 所以我做错了。 Any help or advise would be great and thanks for your time. 任何帮助或建议将是巨大的,感谢您的时间。

EDIT: Working C# Code: 编辑:工作C#代码:

Excel.Range range1 = excelSheet.Range["A36"];
Excel.Characters test = range1.get_Characters(21, 4);
test.Font.Bold = true;

You can't make parts of a string bold, but you can make characters in a cell bold: 您不能将字符串的部分设为粗体,但可以将单元格中的字符设为粗体:

Sub BoldAndBeautiful()
    With Range("A68")
      .Value = "Test 1234 Test"
      .Characters(Start:=1, Length:=4).Font.FontStyle = "bold"
      .Characters(Start:=11, Length:=4).Font.FontStyle = "bold"
    End With
End Sub

Basically do it in two steps. 基本上分两个步骤进行。 First put the text in the cell using the Value of the Range object and then apply the font using the Characters of the Range object. 首先使用Range对象的Value将文本放在单元格中,然后使用Range对象的Characters应用字体。

Note that some systems use an "HTML-type" method to format parts of a string, that is they embed markers to define where formatting starts and stops. 请注意,某些系统使用“ HTML类型”方法来格式化字符串的各个部分,即它们嵌入标记以定义格式化在何处开始和停止。 Excel is not one of them. Excel不是其中之一。

Just adapt this to your c# code. 只需将此修改为您的C#代码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM