while working on an export to Excel I discovered the following problem.
If you create a table where one cell has a line break and you save the document as a txt file it will look like this:
"firstLine<LF>secondLine"<TAB>"secondColoumn"
When I open this file in Excel the line break is gone and the first row has only one cell with the value firstLine
Do you know if it is somehow possible to keep the line breaks?
EDIT: Applies to Excel2010. Don't know if other versions behave different.
EDIT2: Steps to reproduce:
The excel file has now 2 rows which is wrong.
You can import data from a text file into an existing worksheet. Click the cell where you want to put the data from the text file. On the Data tab, in the Get External Data group, click From Text. In the Import Data dialog box, locate and double-click the text file that you want to import, and click Import.
The steps to import a TXT or CSV file into Excel are similar for Excel 2007, 2010, 2013, and 2016: Open the Excel spreadsheet where you want to save the data and click the Data tab. In the Get External Data group, click From Text. Select the TXT or CSV file you want to convert and click Import.
Go to the Data tab > Get External Data > From Text. Then, in the Import Text File dialog box, double-click the text file that you want to import, and the Text Import Wizard dialog will open.
I was finally able to solve the problem! yay :D
CSV:
The german Excel needs a semicolon as a separator. Comma doesn't work.
Note: This is only true when the file is encoded as UTF-8 with BOM at the beginning of the file. If it's ASCII encoded comma does work as a delimiter.
TXT:
The encoding has to be UTF-16LE. Also it needs to be tab delimited.
Important: The files will still be displayed incorrect if you open them with the "File->Open" dialog and "import" them. Draging them into Excel or opening with double click works.
It isn't a problem - in the sense of expected behaviour - this is inherent when you save text as Unicode
or as Text (tab delimited)
If you save the file as unicode and then either
you will see that the cells with linebreaks are surrounded by ""
The example below shows two linebreaks
A1
has an entry separated using Alt+EnterB1
has an enry using the formula CHAR(10)
The picture also shows what notepad sees on a saved Unicode
version
Suggested Workaround 1- Manual Method
In Excel, choose Edit>Replace
Click in the Find What box
Hold the Alt key, and (on the number keypad), type 0010
Replace with a double pipe delimiter
Save as Unicode
Then reverse the process when needed to reinsert the linebreaks
This can be done easily in VBA
Suggested Workaround 2 - VBA alternative
Const strDelim = "||"
Sub LBtoPIPE()
ActiveSheet.UsedRange.Replace Chr(10), strDelim, xlPart
ActiveSheet.UsedRange.Replace "CHAR(10)", strDelim, xlPart
End Sub
Sub PIPEtoLB()
ActiveSheet.UsedRange.Replace strDelim, Chr(10), xlPart
ActiveSheet.UsedRange.Replace strDelim, "CHAR(10)", xlPart
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With