function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sascha DeinertSascha Deinert 

How can I pass the dates to apex class


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.

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 = :currentUserId]); 
        Set<Id> eventidsset = new Set<Id>();
        for(event relation:relatedEventCon.values()){
        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 >
                    date to: <apex:input type="date" value="{!dateto}"/>
        <apex:pageBlock title="Contacts">            
            <apex:pageBlockButtons >
                <apex:commandbutton value="Export contacts to Excel" action="{!exportAll}"/>
VISUAL FORCE PAGE to convert data 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" />