Search code examples
c#openxmlopenxml-sdkspreadsheetml

How to add a cell with an inline string value to a Spreadsheet using the Open XML SDK


When using the Open XML SDK to add cells with non-numeric text to a worksheet, the officially documented method is to use the shared string table. While I recognize the performance and storage efficiency benefits of this approach in the general use case, when creating spreadsheets programmatically, this is a lot of work to do something seemingly simple: The programmer is required to create the shared string table, add entries and maintain entries (including checking if a string already exists in the table), potentially negating the supposed benefits.

The alternative is to use inline string markup, which is mentioned in the documentation, but no example is given. I couldn't find any examples via web search either.


Solution

  • Here's how to create a cell with an inline string value programmatically using the C# OpenXML SDK:

    var inlineString = new InlineString { Text = new Text("Foobar") };
    cellWithInlineStr = new Cell(inlineString) { DataType = CellValues.InlineString };