Skip to content

How to customize exported data in CSV file.

Posted in Java programming

This example is based on the Java and JSF (Primefaces) tech stack, but it can also be used with other frameworks.

A problem description.

There are some cases with table data exporting when exported tables contain unexpected values. Or the values need to be updated on some conditions. Or if we need extra columns/data to combine different UI views in one exported table. There are few examples below.

Primefaces dataExporter is used to export data. A standard configuration looks like

and it gives us a table which was configured accordingly

Let’s export the data (by clicking CSV icon at top right corner) and see what it is in the exported file.

The header of the table will be present as below (format modified to explain):

As we can see there is the first column, which is a checkbox mark in the UI table, is shown like a Java object with text ‘javax.faces.component.UIPanel@393d3f87‘. Let’s mark it as the first thing to customize in the exported file. Probably, we want to see it like ‘Selected’ in the header.

Also there is the last column, which is a button in the UI table, is just empty string (“”) in the header. We don’t need in the file at all. Let’s mark it as the second thing to customize in the exported file.

Now let’s check values in the exported file (format modified to explain). The first row has values in the file:

The first column’s value is “false” (or “true” when it’s selected in the UI table). It’s not a very ‘user-friendly’ message, somebody would like to see it like “Selected” or “Unselected”. It would be the third thing to customize in the exported file.

The ‘Status’ column and the right column with the blue button also have unreadable messages. These values are text versions of Java objects, and additionally hower text for those columns accordingly. Even if we needed an explanation of the column values we would want them to be present in more readable format. In this case we just remove extra text and update the values.

So, that is what the problem is and below is an approach to resolve it.

A solution.

To customize the exported file we should catch it before downloading and update it. We use Primefaces attribute ‘postProcessor‘.

Then we update the file in Java code so it will be customized before downloading. Let’s see the code itself.

And, the updated exported file:

As we can see the values had been updated and the updated file was downloaded.

The same approach can be used to add more columns (values) to csv files or other file types where text values need to be updated.


If you need help with solutions for data analysis and/or any solutions for data management, please contact me sergiitechinfo@gmail.com

Be First to Comment

Add a Comment

Your email address will not be published. Required fields are marked *