Importing CSV with line breaks in Excel 2007

前端 未结 23 2126
悲哀的现实
悲哀的现实 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条回答
  • 2020-11-29 21:06

    If anyone stumbling across this thread and is looking for a definitive answer here goes (credit to the person mentioning LibreOffice:

    1) Install LibreOffice 2) Open Calc and import file 3) My txt file had the fields separated by , and character fields enclosed in " 4) save as ODS file 5) Open ODS file in Excel 6) Save as .xls(x) 7) Done. 8) This worked perfectly for me and saved me BIGTIME!

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

    Excel is incredibly broken when dealing with CSVs. LibreOffice does a much better job. So, I found out that:

    • The file must be encoded in UTF-8 with BOM, so consider this for all the points below
    • The best result, by far, is achieved by opening it from File Explorer
    • If you open it from within Excel there are two possible outcomes:
      • If it has only ASCII characters, it will most likely work
      • If it has non-ASCII characters, it will mess your line breaks
    • It seems to be heavily dependent on the decimal separator configured in the OS's regional settings, so you have to select the right one
    • I would bet that it may also behave differently depending on OS and Office version
    0 讨论(0)
  • 2020-11-29 21:10

    just create a new sheet with cells with linebreak, save it to csv then open it with an editor that can show the end of line characters (like notepad++). By doing that you will notice that a linebreak in a cell is coded with LF while a "real" end of line is code with CR LF. Voilà, now you know how to generate a "correct" csv file for excel.

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

    If you are doing this manually, download LibreOffice and use LibreOffice Calc to import your CSV. It does a much better job of stuff like this than any version of Excel I've tried, and it can save to XLS or XLSX as required if you need to transfer to Excel afterwards.

    But if you're stuck with Excel and need a better fix, there seems to be a way. It seems to be locale dependent (which seems idiotic, in my humble opinion). I don't have Excel 2007, but I have Excel 2010, and the example given:

    ID,Name,Description
    "12345","Smith, Joe","Hey.
    My name is Joe."
    

    doesn't work. I wrote it in Notepad and chose Save as..., and next to the Save button you can choose the encoding. I chose UTF-8 as suggested, but with no luck. Changing the commas to semicolons worked for me, though. I didn't change anything else, and it just worked. So I changed the example to look like this, and chose the UTF-8 encoding when saving in Notepad:

    ID;Name;Description
    "12345";"Smith, Joe";"Hey.
    My name is Joe."
    

    But there's a catch! The only way it works is if you double-click the CSV file to open it in Excel. If I try to import data from text and chose this CSV, then it still fails on quoted newlines.

    But there's another catch! The working field separator (comma in the original example, semicolon in my case) seems to depend on the system's Regional Settings (set under Control Panel -> Region and Language). In Norway, comma is the decimal separator. Excel seems to avoid this character and prefer a semicolon instead. I have access to another computer set to UK English locale, and on that computer, the first example with a comma separator works fine (only on doubleclick), and the one with semicolon actually fails! So much for interoperability. If you want to publish this CSV online and users may have Excel, I guess you have to publish both versions and suggest that people check which file gives the correct number of rows.

    So all the details that I've been able to gather to get this to work are:

    1. The file must be saved as UTF-8 with a BOM, which is what Notepad does when you chose UTF-8. I tried UTF-8 without BOM (can be switched easily in Notepad++), but then double-clicking the document fails.
    2. You must use a comma or a semicolon separator, but not the one that is the decimal separator in your Regional Settings. Perhaps other characters work, but I don't know which.
    3. You must quote fields that contain a newline with the " character.
    4. I've used Windows line-endings (\r\n) both in the text field and as a record separator, that works.
    5. You must double-click the file to open it, importing data from text doesn't work.

    Hope this helps someone.

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

    What just worked for me, importing into Excel directly provided that the import is done as a text format instead as csv format. M/

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

    In my case opening CSV in notepad++ and adding SEP="," as the first line allows me open CSV with line breaks and utf-8 in Excel without issues

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