Woot4Moo
Woot4Moo

Reputation: 24336

Java export csv from server to clients that may not be on same OS

If I have an application sitting on a *nix box and I generate a csv that is then passed off to thin client users what precautions must I take if they are on Windows and want a proper csv?

What I mean is this flow:

UNIX:
1) Generate csv using System.getProperty("line.separator")
2) Pass to thin client

Windows/Unix:
1) Download file from browser
2a) Open in Excel (Windows)
2b) Open in some spreadsheet application

I am not looking for answers that say use Library X, there is a lack of fondness for adding technical risk for libraries that are used for only one project.

Upvotes: 1

Views: 220

Answers (1)

Woot4Moo
Woot4Moo

Reputation: 24336

According to [rfc4180 section 2][1]

there is no formal specification in existence, which allows for a wide variety of interpretations of CSV files. This section documents the format that seems to be followed by most implementations:

  1. Each record is located on a separate line, delimited by a line break (CRLF). For example:

    aaa,bbb,ccc CRLF zzz,yyy,xxx CRLF

  2. The last record in the file may or may not have an ending line break. For example:

    aaa,bbb,ccc CRLF zzz,yyy,xxx

  3. There maybe an optional header line appearing as the first line of the file with the same format as normal record lines. This header will contain names corresponding to the fields in the file and should contain the same number of fields as the records in the rest of the file (the presence or absence of the header line should be indicated via the optional "header" parameter of this MIME type). For example:

    field_name,field_name,field_name CRLF aaa,bbb,ccc CRLF zzz,yyy,xxx CRLF

Therefore System.getProperty("line.separator") is incorrect and \r\n should be used instead. [1]: https://www.rfc-editor.org/rfc/rfc4180#section-2

Upvotes: 1

Related Questions