简体   繁体   中英

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. So it looks like:

Excel单元格

What i tried:

Inside Excel using a range:

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:

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.

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. Excel is not one of them.

Just adapt this to your c# code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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