Importing CSV with line breaks in Excel 2007

前端 未结 23 2128
悲哀的现实
悲哀的现实 2020-11-29 20:49

I\'m working on a feature to export search results to a CSV file to be opened in Excel. One of the fields is a free-text field, which may contain line breaks, commas, quota

相关标签:
23条回答
  • I also had this problem: ie., csv files (comma delimited, double quote delimited strings) with LF in quoted strings. These were downloaded Square files. I did a data import but instead of importing as text files, imported as "from HTML". This time it ignored the LF's in the quoted strings.

    0 讨论(0)
  • 2020-11-29 21:30

    Replace the separator with TAB(\t) instead of comma(,). Then open the file in your editor (Notepad etc.), copy the content from there, then paste it in the Excel file.

    0 讨论(0)
  • 2020-11-29 21:31

    Short Answer

    Remove the newline/linefeed characters (\n with Notepad++). Excel will still recognise the carriage return character (\r) to separate records.

    Long Answer

    As mentioned newline characters are supported inside CSV fields but Excel doesn't always handle them gracefully. I faced a similar issue with a third party CSV that possibly had encoding issues but didn't improve with encoding changes.

    What worked for me was removing all newline characters (\n). This has the effect of collapsing fields to a single record assuming that your records are separated by the combination of a carriage return and a newline (CR/LF). Excel will then properly import the file and recognise new records by the carriage return.

    Obviously a cleaner solution is to first replace the real newlines (\r\n) with a temporary character combination, replacing the newlines (\n) with your seperating character of choice (e.g. comma in a semicolon file) and then replacing the temporary characters with proper newlines again.

    0 讨论(0)
  • 2020-11-29 21:32

    Use Google Sheets and import the CSV file.

    Then you can export that to use in Excel

    0 讨论(0)
  • 2020-11-29 21:32

    +1 on J Ashley's comment. I ran into this problem also. It turns out that Excel requires:

    • A newline character("\n") in the quoted string

    • A carriage return and newline between each row.

    E.g.

    "Test", "Multiline item\n
    multiline item"\r\n
    "Test2", "Multiline item\n
    multiline item"\r\n
    

    I used notepad ++ to delimit each row properly and to only use newlines in the string. Discovered this by creating multiline entries in a blank excel doc and opening the csv in notepad ++.

    0 讨论(0)
提交回复
热议问题