1

Here in my page, I select some picklist field values and based on that i will get the data, Then, When i click the "ExportReport" button,Then data need to save as excel or pdf. I know with pageReference we can do, I gave pageRefrence for "ExportReport" button to save as pdf, But How to give this table in pagreReference page? or can we do in another way?

Page:

<apex:page controller="ReportPageCon">
<apex:form >
              <apex:actionFunction name="dosearchaction" action="{!dosearch}" reRender="showId"/><br/>

  <apex:pageblock title="Report:">
      <apex:pageBlockSection title="Select the Location:">
         <apex:selectList id="pick1" value="{!picklistval1}" size="1" required="true">    
               <apex:outputLabel for="pick1"><b>District:</b></apex:outputLabel>&nbsp;&nbsp;

 <apex:selectOptions value="{!value1}"/>
          </apex:selectList>

          <apex:selectList id="pick2" value="{!picklistval2}" size="1" required="true">
                         <apex:outputLabel for="pick1"><b>Mandal:</b></apex:outputLabel>&nbsp;&nbsp;

              <apex:selectOptions value="{!value2}"/>
          </apex:selectList>
                    <apex:selectList id="pick3" value="{!picklistval3}" size="1" required="true">
                                   <apex:outputLabel for="pick1"><b>Village:</b></apex:outputLabel>&nbsp;&nbsp;

              <apex:selectOptions value="{!value3}"/>
          </apex:selectList>
      </apex:pageBlockSection>
      <apex:pageBlockSection id="showId">
          <apex:pageBlockTable value="{!objList}" var="item">
              <apex:column value="{!item.State__c}"/>
              <apex:column value="{!item.District__c}"/>
              <apex:column value="{!item.Mandal__c}"/>
              <apex:column value="{!item.Village__c}"/>
              <apex:column value="{!item.First_Name__c}"/>
                            <apex:column value="{!item.Aadhar_ID__c}"/>
                                          <apex:column value="{!item.Gender__c}"/>
                                          <apex:column value="{!item.Date_Of_Birth__c}"/>
                            </apex:pageblockTable>
      </apex:pageBlockSection>
      <apex:commandButton value="Generate Report" action="{!dosearch}" onclick="dosearchaction();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <apex:commandButton value="Export Report" action="{!ExportReport}"/>
  </apex:pageblock>
  </apex:form>
</apex:page>

Apex Class:

public class ReportPageCon {

public String picklistval1 {get;set;}
public String picklistval2 {get;set;}
public String picklistval3 {get;set;}
public List<object__c> objList{get;set;}

public ReportPageCon() {
    objList=new List<object__c>();
}

public List<SelectOption> getvalue1(){

    List<SelectOption> options = new List<SelectOption>();

    Schema.DescribeFieldResult fieldResult = object__c.District__c.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for( Schema.PicklistEntry f : ple)
    {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
    }       
    return options;
}

public List<SelectOption> getvalue2(){

    List<SelectOption> options = new List<SelectOption>();

    Schema.DescribeFieldResult fieldResult = object__c.mandal__c.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for( Schema.PicklistEntry f : ple)
    {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
    }       
    return options;
}
    public List<SelectOption> getvalue3(){

    List<SelectOption> options = new List<SelectOption>();

    Schema.DescribeFieldResult fieldResult = object__c.Village__c.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    for( Schema.PicklistEntry f : ple)
    {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
    }       
    return options;
}
public void dosearch(){
    objList = [Select First_Name__c,Mobile_Number__c,Gender__c , Name,state__c,District__c,mandal__c,Age__c,Village__c from object__c Where District__c =: picklistval1 and mandal__c=:picklistval2 and village__c=:picklistval3]; //change according to your criteria

}
    public pageReference ExportReport()
    {

        PageReference myVFPage = new PageReference('/apex/ExportReportPage');

    return myVFPage;
    }
}
0

your Ape page class and pages are perfect, just need to add little more .please follow the below step : -

  1. In your ExportReportPage by using the same controller just built the same pageblock table like below : -

    `<apex:page controller="ReportPageCon" id="MainPage" showHeader="false" cache="true" contentType="application/x-pdf#Here-is-your-filename.pdf"`>  
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!objList}" var="item">
        <apex:column value="{!item.State__c}"/>
        <apex:column value="{!item.District__c}"/>
        <apex:column value="{!item.Mandal__c}"/>
        <apex:column value="{!item.Village__c}"/>
        <apex:column value="{!item.First_Name__c}"/>
        <apex:column value="{!item.Aadhar_ID__c}"/>
                  <apex:column value="{!item.Gender__c}"/>
                  <apex:column value="{!item.Date_Of_Birth__c}"/>
        </apex:pageblockTable>          
    </apex:pageBlock>
    </apex:form>                
     </apex:page>
    

As you are using same controller you can use the objList list which is already being populated in first page.

In the second page you can use contentType="application/x-pdf#Here-is-your-filename.pdf" ,so whenever the second page gets loaded it will download as PDF.

If you want to download as word then do the below changes in contentType, like contentType="application/msWord#yourFile.doc"

|improve this answer|||||
  • Thank you, ur code is working fine. It is saving as Exel but it is not saving as pdf. I have tried from developer docs also. but not getting. Can you help on this. I am getting error with above content type: Failed to load PDF document RELOAD – Ravindar Admin May 1 '17 at 7:33

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.