i All,
I trying to download program for contact object , the user click the “Download” custom button it should download all the records from the object. This download file should in text format(.txt).when i download a file it display like this
johnkumarjane but i want to display like this in text file
john kumar jane
how to add break line
<apex:page Controller="contactquery" contentType="text/plain/#emp.txt" cache="true">
<apex:repeat value="{!cs}" var="contact">
<apex:outputText value="{!contact.Name}" escape="false"/>
<apex:outputText value="{!contact.Id}" escape="false"/>
</apex:repeat>
</apex:page>
public class contactquery{
public List<Contact> cs{get; set;} public string empId; public contactquery() { cs = new List<Contact>(); for (Contact c : [Select id, Name from Contact ]) { cs.add(c); } }
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Its the use of the apex:outputtext component that causes everything to remain on the same line. If you simply use the merge fields, the line breaks work as expected. E.g.
<apex:page Controller="ContactQuery" contentType="text/plain/#emp.txt" cache="true"> <apex:repeat value="{!cs}" var="contact"> {!contact.Name} {!contact.Id} </apex:repeat> </apex:page>
Opening the output file in vi, shows the following – the line numbers indicate that each entry is on a new line:
Elsewhere you have indicated that you are using notepad to open the output file – the problem here is that notepad expects each line to contain a carriage return followed by a line feed, whereas the text file simply contains a line feed. You can handle this server side by building a string that contains the contact elements you are interested in plus the carriage return line feed characters.
Revised controller (updated to include fixed size name):
public class ContactQuery{ public List<String> contstr {get; set;} public string empId; public contactquery() { contStr=new List<String>(); for (Contact c : [Select id, Name from Contact ]) { String nameStr=c.Name.rightPad(40); contStr.add(nameStr + ' ' + c.Id + 'r'); } } }
Revised page:
<apex:page Controller="ContactQuery" contentType="text/plain/#emp.txt" cache="true"> <apex:repeat value="{!contstr}" var="str"> {!str} </apex:repeat> </apex:page>
Opening the output from this page in vi shows that there is a carriage return at the end of the line (the ^M character), followed by a line feed, and each name is padded to 40 characters:
Method 2
It working correctly, have changed the code for (Contact c : [Select Name,Phone,Email,Fax from Contact ]) { cs.add(c.Name.rightPad(40)+ c.Email.rightPad(30)+ c.Phone.rightPad(20)+c.Fax.rightPad(20) + ‘r’); }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0