CSV Separator Tutorial
In this tutorial you'll learn how to use different separators for
CSV generated reports.
A comma-separated values (CSV) file is, as its name
suggests a file with values separated by commas. Simple, right? Not so.
For some strange reasons, some applications defined their own
separators (sometimes even guessing you'll expect a different
separator than a comma according to your local language). In the case
you need to support a distinct separator to be able to always use
the wrong-application, or if you just want to be like Uncle Bill and
be cool using your comma-separated-values-not-separated-by-commas
(CSVNSBC) file, you won't need to stop using Nerval Reports.
The solution is as simple as inherit
CSVReportGenerator class and override the function
getSeparator() returning your needed separator.
public class CSVNSBCReportGenerator extends CSVReportGenerator {
private static final String MY_NOT_COMMA_SEPARATOR = "_SEP_";
@Override
protected String getSeparator() {
return MY_NOT_COMMA_SEPARATOR;
}
}
As you can see, it returns a
String, so your
separator isn't restricted to be a single character one. Also, you can
add any complex code to guess which separator to use according to the
user's language you detected:
getSeparator() is only called
once per CSVReportGenerator derived instance (obviously, if it should
be a common behaviour to all instances, you should set it statically
and only call the check once per program use).