function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saptarshi SahaSaptarshi Saha 

Creating Excel CSV file with contents in separate columns using only Apex

I am able to created an excel csv file attaching it with my mail. But all the information is being put inside the first column. I want to place the separate field values in separate columns. Can anyone suggest how can I do it using only Apex (as that is my strict requirement) and not using VisualForce or any plugins or xml file ?
Best Answer chosen by Saptarshi Saha
Ashwani PradhanAshwani Pradhan
You can set a variable like below -
String content = 'Sr No., Col A, Col B \n';

then in loo use it like -
content += i + ',' + Val Col A + ',' + Val Col B + '\n';

If your column value with comma then keep them in double quote in above string concatenation.

Hope this helps :)

All Answers

Ashwani PradhanAshwani Pradhan
You can set a variable like below -
String content = 'Sr No., Col A, Col B \n';

then in loo use it like -
content += i + ',' + Val Col A + ',' + Val Col B + '\n';

If your column value with comma then keep them in double quote in above string concatenation.

Hope this helps :)
This was selected as the best answer
Saptarshi SahaSaptarshi Saha
What is loo ?
Saptarshi SahaSaptarshi Saha
Using ',' as delimiter is not working
Saptarshi SahaSaptarshi Saha
@Ashwani thank you. Yes your solution worked. I accidentally was saving the file name with .xls extension. That is why the delimiter was not working.
Ashwani PradhanAshwani Pradhan
Good that worked! Sorry for the typo.