I have a need to write an “empty” CSV file that includes only the header row information in the case that there’s no data to populate the file. Does anyone know a good way to do this? Anything to do with the CSV formatter and the header row options doesn’t seem to be working out. It’s always writing a row of data with blanks for each element separated by the delimiter. Appreciate any thoughts, thanks.
I don’t think it’s possible to configure the CSV Formatter to do this at the moment. You might need to do something like the following to detect when there is no data:
So, on the ‘No Data’ branch, you can have a Mapper that defines the headers and a CSVFormatter that writes the document to the file. Not great, I wish I had a better answer.
Thanks tstack. That’s what we ended up implementing. Agreed, it is not great, especially for a document with dozens of header columns.