How to insert line break within OPENXML spreadsheet cell?
Asked Answered
M

3

12

I'm currently using something like this to insert inline string in a cell :

new Cell() 
{ 
    CellReference = "E2", 
    StyleIndex = (UInt32Value)4U, 
    DataType = CellValues.InlineString, 
    InlineString = new InlineString(new Text( "some text")) 
}

But \n doesn't work to insert line break, how can i do this?


The response

new Cell(
         new CellValue("string \n string")
        ) 
    { 
        CellReference = "E2", 
        StyleIndex = (UInt32Value)4U, 
        DataType = CellValues.String         
    }
Mcclurg answered 8/4, 2009 at 22:3 Comment(0)
P
6

Try CellValues.String instead of CellValues.InlineString.

Parade answered 9/4, 2009 at 1:45 Comment(1)
Although technically CellValues.String is only for formulas. #6469283Balkan
M
13

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.

Malevolent answered 15/11, 2011 at 19:53 Comment(2)
How do you apply the CellFormat object or Alignment object to a given cell?Squalid
This doesn't work for me. I just get an error on the line with \r\n and excel won't open the sheet with the text saying it is corruptedPatrolman
P
6

Try CellValues.String instead of CellValues.InlineString.

Parade answered 9/4, 2009 at 1:45 Comment(1)
Although technically CellValues.String is only for formulas. #6469283Balkan
E
0

My solution was: when creating CellFormat set WrapText = true.

new Alignment
{
    Horizontal = HorizontalAlignmentValues.Left,
    Vertical = VerticalAlignmentValues.Center,
    WrapText = true, //This setting
};
Evictee answered 16/4, 2024 at 0:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.