I have a VFP which functions as a CSV download. In my controller I return a simple SOQL list from a custom Customer object. I need to modify the Name being returned to remove all commas and then add it back to my list as it is breaking the csv file. I haven't been able to figure out how to modify the string and add it back to the list with no commas.
Example would be |Customer_ID__c|Name|Modified Name (Desired)| |--------------|----|-----------------------| |1|Bobs Company (Atlanta, GA)|Bobs Company (Atlanta GA)| |2|Jims Company (San Francisco, CA)| Jims Company (San Francisco CA)|
public inherited sharing class CSVExportController {
public String customerId {
get {
if (customerId == null) {
customerId = System.currentPageReference().getParameters().get('id');
}return customerId;
}set;
}
public List<SPCustomer__c> getCustomerExport {
get{
if(getCustomerExport == null){
getCustomerExport = [SELECT Customer_ID__c, Name from SPCustomer__c where Id = :this.customerId limit 1];
}return getCustomerExport;
}set;
}
public String COMMA {
get {
return String.fromCharArray(new List<Integer> { 44 });
}
}
public String CRLF {
get {
return '\r';
}
}
public CSVExportController() {}
}
Visualforce Page:
<apex:page Controller="CSVExportController" showHeader="false" sidebar="false" cache="true" contentType="application/octet-stream#CustExport.csv">
<!-- Header Row -->
<apex:outputText value="Cust_Number"/><apex:outputText value="{!COMMA}"/>
<apex:outputText value="Cust_Name"/><apex:outputText value="{!COMMA}"/>
<apex:outputText value="{!CRLF}"/>
<!-- Data rows -->
<apex:repeat value="{!getCustomerExport}" var="cust">
<apex:outputText value="{!cust.Customer_ID__c}"/><apex:outputText value="{!COMMA}"/>
<apex:outputText value="{!cust.Name}"/><apex:outputText value="{!COMMA}"/>
<apex:outputText value="{!CRLF}"/>
</apex:repeat>
</apex:page>
'\r'
isn't CRLF, it's just CR. You want'\r\n'
if you want CRLF. Also,COMMA
could literally just return','
.