I'm using the Aspose library to create an Excel document. Somewhere in some cell I need to insert a new line between two parts of the text.
I tried "\r\n" but it doesn't work, just displays two square symbols in cell. I can however press Alt+Enter to create a new line in that same cell.
How do I insert a new line programmatically?
Using PEAR 'Spreadsheet_Excel_Writer' and 'OLE':
Only way I could get "
\n
" to work was making the cell$format->setTextWrap();
and then using "\n
" would work.If you are getting the text from DB then:
You need to insert the character code that Excel uses, which IIRC is 10 (ten).
EDIT: OK, here's some code. Note that I was able to confirm that the character-code used is indeed 10, by creating a cell containing:
...and then selecting it and executing this in the VBA immediate window:
So, the code you need to insert that value into another cell in VBA would be:
(since vbLf is character code 10).
I know you're using C# but I find it's much easier to figure out what to do if you first do it in VBA, since you can try it out "interactively" without having to compile anything. Whatever you do in C# is just replicating what you do in VBA so there's rarely any difference. (Remember that the C# interop stuff is just using the same underlying COM libraries as VBA).
Anyway, the C# for this would be:
Spot the difference :-)
EDIT 2: Aaaargh! I just re-read the post and saw that you're using the Aspose library. Sorry, in that case I've no idea.
"\n" works fine. If the input is coming from a multi-line textbox, the new line characters will be "\r\n", if this is replaced with "\n", it will work.
From the Aspose Cells forums: How to use new line char with in a cell?
After you supply text you should set the cell's IsTextWrapped style to true
You can use Chr(13). Then just wrap the whole thing in Chr(34). Chr(34) is double quotes.