2

I am trying a download file program which should be of the Fixed width Text file format.

For example:-

  1. My object name is College and it has fields(length) namely Name(10),rollno(7),marks(3).
  2. The query should fetch the data from the above object and download it as a fixed width text file display according to the field length in a standard text file.
Name       Rollno mrk
sample     142345 243  
demo       1938452100

The text file should display the records in the above format.

Does anyone have a solution to this issue? Kindly help me with the links or sample code snippets if possible. Thanks for your time.

VF PAGE:

<apex:page standardController="SAP_SD__c" extensions="Text_File_Download" sidebar="false" action="{!fetch}" contentType="text/plain/#emp.txt" cache="false">
Employee Number : {!emp.Name}
Employee Name : {!emp.AccNo__c}
City : {!emp.PoNo__c}
Email : {!emp.SalesOrdNo__c}

</apex:page>

APEX CLASS:

public class Text_File_Download
{ 
public Text_File_Download(ApexPages.StandardController controller) {

}


public String empId;
public SAP_SD__c emp {get;set;}

public Text_File_Download()
{

}

public void fetch()
{

    empId = Apexpages.currentpage().getparameters().get('Id');
    emp = [SELECT Name, AccNo__c, PoNo__c, SalesOrdNo__c FROM SAP_SD__c WHERE ID =: empId];

}
}
1

You can perform this using the RPAD function in Visualforce. It is described as follows...

enter image description here

For example...

<apex:page standardController="Test__c" sidebar="false" contentType="text/plain/#emp.txt" cache="false">
{!RPAD('Name', 10)}{!RPAD('Date', 10)}
{!RPAD(Test__c.Name, 10)}{!RPAD(TEXT(Test__c.Date__c), 10)}
</apex:page>

Note: My example above does not use an Apex controller to read as it refers solely to the StandardController record. Looking at your quesiton vs the sample code you have given, its hard to tell if you want single or multiple records? Since your example shows many rows, yet 'emp' is not defined as a list? While this is not directly part of your question I thought I'd point this out and suggest that if you do want multiple rows you can make 'emp' an array and use the apex:repeat tag to output the rows.

3

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.