How To make some text bold in cell using OpenXml

You need to use separate Run elements for the differently styled pieces of text. You can add the bold by creating a RunProperties element and adding a Bold element to that.

The following code will work on an existing spreadsheet that has no rows (note I haven't added the code for merging as that just adds complication - if you need help with that then please see my answer here)

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, true))
{
    WorkbookPart workBookPart = spreadsheetDocument.WorkbookPart;

    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();

    //create a row
    Row row1 = new Row() { RowIndex = 1U };

    //create a new inline string cell
    Cell cell = new Cell() { CellReference = "A1" };
    cell.DataType = CellValues.InlineString;

    //create a run for the bold text
    Run run1 = new Run();
    run1.Append(new Text("ABC"));
    //create runproperties and append a "Bold" to them
    RunProperties run1Properties = new RunProperties();
    run1Properties.Append(new Bold());
    //set the first runs RunProperties to the RunProperties containing the bold
    run1.RunProperties = run1Properties;

    //create a second run for the non-bod text
    Run run2 = new Run();
    run2.Append(new Text(Environment.NewLine + "XYZ") { Space = SpaceProcessingModeValues.Preserve });

    //create a new inline string and append both runs
    InlineString inlineString = new InlineString();
    inlineString.Append(run1);
    inlineString.Append(run2);

    //append the inlineString to the cell.
    cell.Append(inlineString);

    //append the cell to the row
    row1.Append(cell);

    sheetData.Append(row1);
}

Comments

  1. Seth

    • 2019/10/13

    You need to use separate Run elements for the differently styled pieces of text. You can add the bold by creating a RunProperties element 

  2. Caspian

    • 2015/2/7

    1 Answer1. You need to use separate Run elements for the differently styled pieces of text. You can add the bold by creating a RunProperties element and adding a Bold element to that.

  3. Kolten

    • 2020/5/18

    Cell cell = new Cell() { CellReference = "A1" };. 14. cell.DataType = CellValues.InlineString;. 15. ​. 16. //create a run for the bold text.

  4. Kaysen

    • 2016/4/3

    i have try to make bold the specific text using. Bold fbld = new Bold(); but it will make bold hall cell. Here in above image there is some bold text into the cell.

  5. Alec

    • 2020/5/25

    Hi, you can use following code to make a cell bold. Replace the <FilePath> with the path of your Excel workbook. Also, the following code makes 

  6. Bronson

    • 2018/12/1

    how make header textbox bold in excel file with open xml sdk 2.0 How to convert an excel sheet to a specific format in ASP.NET Create hyperlink and populating it in excel sheet using Open XML sdk

  7. Sage

    • 2019/2/21

    Apply bold formatting to the run in the Word Processing Document using Open XML SDK 2.0 · { · class Program · { · static void Main(string[] args) · {.

  8. Kye

    • 2019/2/9

    Bold bold = new Bold (); bold.Val = OnOffValue.FromBoolean(true); runProperties.AppendChild(bold); run.AppendChild(new Text ("Welcome!!!!!")); doc.MainDocumentPart.Document.Save(); } } }}

  9. Flores

    • 2015/4/16

    Viewing 3 posts - 1 through 3 (of 3 total) I'm not able to figure out how to set a bold text into a Spreadsheet when creating it.

  10. Kairo

    • 2015/12/9

    new CellFormat(){FontId = 1, FillId = 5, BorderId = 1, ApplyFont = true, ApplyFill = true, ApplyBorder = true} // Index 6 - Bold w/lt gray fill and borders. ) ); // Return StyleSheet } There are some important things to note: It is important to reference the style when you create the cell in the WorksheetData.

  11. Jimenez

    • 2020/3/16

    Net with the first row as the header with text in Bold with would like to create a table in word document programatically using Open XML 

  12. Laci

    • 2019/6/9

    This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically apply a style to a paragraph within a word processing document. It contains an example ApplyStyleToParagraph method to illustrate this task, plus several supplemental example methods to check whether a style exists, add a new style, and add the styles

  13. Colombo

    • 2015/10/17

    Create a Style. The following code shows how to create and apply cell style. c#; VB.NET; uwp; asp.net core 

  14. Alistair

    • 2018/5/31

    Hi Behnam, Thanks for your post. It really helped me to understand the OpenXML better. I have a requirement. I need to find a particular row of an excel sheet using one column value and edit the other columns of that row.[Similar to editing an SQL Table item using primary key].

  15. Alistair

    • 2018/8/14

    <summary> /// Create an stylesheet to use in excel files /// </summary> OpenXml.Spreadsheet.Font() { Bold = new Bold(), FontSize = new FontSize() { Val 

  16. Leroy

    • 2019/9/26

    i have try to make bold the specific text using Bold fbld = new Bold(); but it will make bold hall cell. Here in above image there is some bold text into 

Comments are closed.

Recent Posts