7

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);          

   }

}

}

6

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:

enter image description here

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:

enter image description here

1

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'); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.