Skip to main content
Sascha Deinert a posé une question dans #Salesforce Developer

Hi,  I need to export contact data which are related to the current user into an excel file. This works fine, but now I need the export only for a time range which the user can set at the visual force page. How can I pass the dates (from/to) from the visual force to the apex class?  It is possible to export the data in xlsx format? Currently it is xls format.

 

APEX CLASS

APEX CLASS

public with sharing class ExportToExcel {

public List<Contact> contactlist{get;set;}

public Id currentUserId { get; set;}

public Date datefrom {get;set;}

public Date dateto {get;set;}

public String xlsHeader {

get {

String strHeader = '';

strHeader += '<?xml version="1.0"?>';

strHeader += '<?mso-application progid="Excel.Sheet"?>';

return strHeader;

}

}

public ExportToExcel(){

currentUserId = UserInfo.getUserId();

If(datefrom != Null && dateto != Null) {

Map<Id, Event> relatedEventCon = new Map<Id, Event>([select whoId from Event where activitydate >= :datefrom and activitydate <= :dateto and subject = 'BookingTime AN Termin' and owner.id = :currentUserId]);

Set<Id> eventidsset = new Set<Id>();

for(event relation:relatedEventCon.values()){

eventidsset.add(relation.whoid);

}

contactlist = [select Salutation, Firstname, Lastname, Birthdate, E_Mail_privat__c, Mobiltelefon_privat__c, Account.Name from Contact where Id IN :eventidsset];

}

}

public Pagereference exportAll(){

Date datefrom = datefrom;

Date dateto = dateto;

return new Pagereference('/apex/beratertag3out');

}

}

 

VISUAL FORCE PAGE to set the date range (ExportToExcel)

<apex:page controller="ExportToExcel" docType="html-5.0" id="vfpage">

<apex:form >

<apex:pageBlock >

<apex:pageBlockSection >

<apex:pageBlockSectionItem >

date from: <apex:input type="date" value="{!datefrom}"/>

</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >

date to: <apex:input type="date" value="{!dateto}"/>

</apex:pageBlockSectionItem>

</apex:pageBlockSection>

</apex:pageBlock>

<apex:pageBlock title="Contacts">

<apex:pageBlockButtons >

<apex:commandbutton value="Export contacts to Excel" action="{!exportAll}"/>

</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>

</apex:page>

 

VISUAL FORCE PAGE to convert into excel file (berater3out)

<apex:page controller="ExportToExcel" contentType="txt/xml#myTest.xls" cache="true">

<apex:pageBlock >

<apex:pageBlockTable value="{!contactlist}" var="con">

<apex:column value="{!con.Salutation}" headerValue="Anrede"/>

<apex:column value="{!con.Firstname}" headerValue="Vorname" />

<apex:column value="{!con.Lastname}" headerValue="Nachname" />

<apex:column value="{!con.Birthdate}" headerValue="Geburtsdatum" />

<apex:column value="{!con.E_Mail_privat__c}" headerValue="E-Mail-Adresse" />

<apex:column value="{!con.Mobiltelefon_privat__c}" headerValue="Telefonnummer" />

<apex:column value="{!con.Account.Name}" headerValue="Firma" />

</apex:pageBlockTable>

</apex:pageBlock>

</apex:page>

 

Thanks,

Sascha

 

#Salesforce Developer  #Salesforce Development  #Microsoft Excel  #Data Export

5 réponses
  1. 23 sept. 2021, 12:20

    ok, I will try to use the xls file, but how can I pass the date from the visual force to the apex controller?

0/9000