I’m trying to create a script to return concat lines from the DB and with c# populate an excel file with the text.
I’ve managed every thing, just can’t find a solution for the line feed…
Tried char(10)+char(13) and many other.
I know that in excel if one want to add new line, he show use the alt+enter. is there are code of that to add to my tsql script to get the linefeed in the excel file?
Thank you,
Erez
I had the same problems, but mine is a copy-and-paste from the SQL Server Management Studio results window into an Excel file. It’s the same issue, just more direct.
For those folks that don’t understand the problem, here’s a repro. Run the following in SSMS and copy-and-paste the results into Excel. Set the cell to wrap text.
SELECT 'Hello' + CHAR(10) + 'World'
You’ll see Hello on one cell and World on the cell below it. What we’re trying to see is
Hello
World
in one cell with a line break.
My solution was to surround the result in quotes. There are several ways to do this. I found this to look the cleanest:
SELECT QUOTENAME('Hello' + CHAR(10) + 'World', '"')
The result is “Hello World” in the SSMS results window. But when pasted into Excel and the cell set with wrap text, I would get the desired result.
Answer:
Cr+Lf is actually 13+10 rather than 10+13.
If that does not work, a single char(10) will.
Answer:
The solution for my problem what using
in the tsql and decoding it in the c#, as i am creating a html table from the DataSet and then converting the html table into excel stylesheet.
SQL:
SELECT br_in.BIText + '<br>'
C#:
StrReport.Append("<td>" + HttpUtility.HtmlDecode(ds.Tables[k].Rows[i][j].ToString()) + "</td>");
This is the only thing that worked in my case and it even make sense if you think about it….
Thank you,
Erez
Answer:
This was the only way I could get it to work
SQL: select ‘=”Hello”&CHAR(10)&”World”‘
In Excel I also had to set the cell with wrap text.
Answer:
In my case it worked with following
string text = String.Format(“\”{0}\””, text);
by setting your test to this format it will create alt+Enter in Excel.
Tags: .net, asp.net, asp.netsql, excel, tsql