{"id":73,"date":"2025-02-23T17:00:11","date_gmt":"2025-02-23T17:00:11","guid":{"rendered":"https:\/\/infotechville.com\/blog\/?p=73"},"modified":"2025-02-23T17:00:12","modified_gmt":"2025-02-23T17:00:12","slug":"how-to-customize-exported-data-in-csv-file","status":"publish","type":"post","link":"https:\/\/infotechville.com\/blog\/index.php\/2025\/02\/23\/how-to-customize-exported-data-in-csv-file\/","title":{"rendered":"How to customize exported data in CSV file."},"content":{"rendered":"\n<p>This example is based on the Java and JSF (Primefaces) tech stack, but it can also be used with other frameworks.<\/p>\n\n\n\n<p><strong>A problem description.<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Primefaces dataExporter is used to export data. A standard configuration looks like<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"172\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-2-1024x172.png\" alt=\"\" class=\"wp-image-82\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-2-1024x172.png 1024w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-2-300x50.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-2-768x129.png 768w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-2.png 1195w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>and it gives us a table which was configured accordingly<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"187\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5-1024x187.png\" alt=\"\" class=\"wp-image-86\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5-1024x187.png 1024w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5-300x55.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5-768x140.png 768w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5-1536x280.png 1536w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-5.png 1780w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s export the data (by clicking CSV icon at top right corner) and see what it is in the exported file.<\/p>\n\n\n\n<p>The header of the table will be present as below (format modified to explain):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"492\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-4.png\" alt=\"\" class=\"wp-image-84\" style=\"width:323px;height:auto\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-4.png 628w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-4-300x235.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p>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 &#8216;<em>javax.faces.component.UIPanel@393d3f87<\/em>&#8216;. Let&#8217;s mark it as the first thing to customize in the exported file. Probably, we want to see it like &#8216;Selected&#8217; in the header.<\/p>\n\n\n\n<p>Also there is the last column, which is a button in the UI table, is just empty string (&#8220;&#8221;) in the header. We don&#8217;t need in the file at all. Let&#8217;s mark it as the second thing to customize in the exported file.<\/p>\n\n\n\n<p>Now let&#8217;s check values in the exported file (format modified to explain). The first row has values in the file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"982\" height=\"465\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-6.png\" alt=\"\" class=\"wp-image-88\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-6.png 982w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-6-300x142.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-6-768x364.png 768w\" sizes=\"auto, (max-width: 982px) 100vw, 982px\" \/><\/figure>\n\n\n\n<p>The first column&#8217;s value is &#8220;false&#8221; (or &#8220;true&#8221; when it&#8217;s selected in the UI table). It&#8217;s not a very &#8216;user-friendly&#8217; message, somebody would like to see it like &#8220;Selected&#8221; or &#8220;Unselected&#8221;. It would be the third thing to customize in the exported file.<\/p>\n\n\n\n<p>The &#8216;Status&#8217; 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.<\/p>\n\n\n\n<p>So, that is what the problem is and below is an approach to resolve it.<\/p>\n\n\n\n<p><strong>A solution.<\/strong><\/p>\n\n\n\n<p>To customize the exported file we should catch it before downloading and update it. We use Primefaces attribute &#8216;<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">postProcessor<\/mark><\/em>&#8216;. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"200\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-9.png\" alt=\"\" class=\"wp-image-95\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-9.png 965w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-9-300x62.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-9-768x159.png 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/figure>\n\n\n\n<p>Then we update the file in Java code so it will be customized before downloading. Let&#8217;s see the code itself.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"748\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-8.png\" alt=\"\" class=\"wp-image-93\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-8.png 785w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-8-300x286.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-8-768x732.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/figure>\n\n\n\n<p>And, the updated exported file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" src=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-10-1024x415.png\" alt=\"\" class=\"wp-image-97\" srcset=\"https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-10-1024x415.png 1024w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-10-300x121.png 300w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-10-768x311.png 768w, https:\/\/infotechville.com\/blog\/wp-content\/uploads\/2025\/02\/image-10.png 1450w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As we can see the values had been updated and the updated file was downloaded.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>If you need help with solutions for data analysis and\/or any solutions for data management, please contact me <a href=\"mailto:sergiitechinfo@gmail.com\">sergiitechinfo@gmail.com<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/infotechville.com\/blog\/index.php\/2025\/02\/23\/how-to-customize-exported-data-in-csv-file\/\">Read more<span class=\"screen-reader-text\">How to customize exported data in CSV file.<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-java-programming","entry"],"_links":{"self":[{"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":15,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/73\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infotechville.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}