I have looked for similar questions on stackoverflow but I haven't found any. I want to export a table in CSV format so that it can be imported into Excel. Each cell contains text and each row has the same number of columns. The format I have tried is the following:
"d1"|"d2"|"d3"|"d4"
where d1, d2, d3, d4 are the original strings I want to put in each cell. I have the following problems:
Do you have any idea how to fix the above issues? Is there some online documentation regarding these specific problems? I been searching since yesterday but did not find anything.
Excel supports newlines in values. For example, using the Excel user interface, you can get "foo\\n\\bar\\nbaz"
into a cell by typing Alt-Enter for each line-break.
The tricky thing about Excel is that in locales where the comma is used as a decimal point, Excel uses a colon as the field delimiter. There is no universal/international format that any Excel will read.
I'd be very surprised if there wasn't a package in Java for reading/writing CSV files. Python has one that allows you to specify the delimiter, quote char, record separator, etc on both input and output.
However if you want to write your own, follow this pseudocode for each row that you want to write:
for each field in the row:
if field contains quotechar:
double all quotechars in field
field = quotechar + field + quotechar
else if field contains delimiter, CR, or LF:
field = quotechar + field + quotechar
else:
avoid waste of space and ugly visual impact by NOT doing unneeded quoting
join field strings separated by delimiter
append CR LF
write the row string using binary mode (so Windows runtime doesn't give you 2xCR)
Note carefully (1) all of the above is premised on 8-bit characters (2) I have avoided using the ambigous term "newline".
I find CSV is best done with comma separation, and quoting values so commas in values aren't misinterpreted. Quoting quotes is done with double quoting. So the following four values:
one
two
three with "quoted" value
four
becomes:
one,two,"three with ""quoted"" value",four
I don't believe it's possible for standard CSV implementations to support newlines in values; particularly not in Excel. Try creating a cell in Excel with newlines (is that even possible?) and saving as CSV to see if that works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.