How to insert line break within OPENXML spreadsheet cell?

You need to do two things:

1.) Mark the cell as "Wrapped Text". You can do this in the spreadsheet by hand if you are using an existing spreadsheet as your template. Just right-click on the cell(s) and select "Format Cells..", click on the "Alignment" tab and check the "Wrap Text" checkbox.
OR... You can set the CellFormat programmatically. If you have a CellFormat object called "cf ", you would do something like this:

cf.ApplyAlignment = true;//Set this so that Excel knows to use it.
if (cf.Alignment == null)//If no pre-existing Alignment, then add it.
  cf.Alignment = new Alignment() { WrapText = true };
Alignment a = cf.Alignment;
if (a.WrapText == null || a.WrapText.Value == false)
  a.WrapText = new BooleanValue(true);//Update pre-existing Alignment.

2.) You shouldn't use "\n", instead you should be using the standard carriage-return + line-feed combination: "\r\n"
If you are mixing the two (i.e. "\n" without the preceeding "\r" and "\r\n"), this should fix it before populating the cell value:

sHeaderText = sHeaderText.Replace("\r\n", "\n").Replace("\n", "\r\n");

I, myself, do not use CellValues.String or even CellValues.InlineString.
Instead I build my text cells using CellValues.SharedString (just like Excel does).
For Bool I use "CellValues.Boolean", and for all others (numerics and dates) I do not set the cell's DataType to anything - because that is what Excel does when I look at the markup it creates.


Try CellValues.String instead of CellValues.InlineString.