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
SF_MonkeySF_Monkey 

Setting page orientation when VF is exported to excel

I am trying to format excel file that is exported for VisualForce page. I am able to export the data in a table with cell width that I need. For printing, I want to set the page orientation:Landscape and width of the page to fit one page. I am unable to do that. I have two VF pages, both are for the same table but different approach. Any solution in either of the approaches will be helpful

VF 1
<apex:page controller="accMgrCtrl" id="pg" standardStylesheets="false" contentType="application/vnd.ms-excel#DM-Reached-Last90Days-{!TODAY()}.xls" cache="true" language="en-US">

   <head>
    <style>
        @page {
            size:landscape;
        }
    </style>
</head>  
    <table style="border:1px solid; font-size:16px;">

        <tr style="text-align:center; border:1px solid; ">
       <th>Name</th>
       <th>Next Follow-Up Date</th>
       <th>Next Follow-Up Action</th>
        <th>Primary POC</th>
       <th>Last DM Reached</th>
     </tr> 

             <apex:repeat value="{!accList}" var="a">
      <tr style="text-align:center; border:1px solid; width:auto; word-wrap: break-word;">
      <td><apex:outputLink value="/{!a.id}">{!a.Name}</apex:outputLink></td>
          <td data-cell-type="date"><apex:outputField value="{!a.NEXT_FOLLOW_UP_DATE__c}"/></td>
      <td style=" word-wrap: break-word; "><apex:outputField value="{!a.Next_Action_Follow_Up__c}"/></td>
      <td><apex:outputField value="{!a.Primary_POC__c}"/></td>
        <td data-cell-type="date">  <apex:repeat value="{!a.Prospecting_Activities__r}" var="t">
           <apex:outputField value="{!t.Activity_Date__c }"/>
          </apex:repeat>
          </td>
      </tr>
    </apex:repeat> 

    </table>

</apex:page>

VF 2
<apex:page controller="accMgrCtrl" contentType="application/vnd.ms-excel#DM-Reached-Last90Days-{!TODAY()}.xls" standardStylesheets="false" cache="true" language="en-US">

    <apex:outputText value="{!xlsHeader}"/>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:html="http://www.w3.org/TR/REC-html40">

         <Styles>
             <Style ss:ID="s1">
             <Alignment/>
             <Borders/>
             <Font ss:Bold="1"/>
             <Interior/>
             <NumberFormat/>
             <Protection/>
             </Style>
         </Styles>

         <Worksheet ss:Name="Accounts">
             <Table x:FullColumns="5" x:FullRows="1">
                 <Column ss:Width="170"/>
                 <Column ss:Width="280"/>
                 <Column ss:Width="330"/>
                 <Column ss:Width="330"/>
                 <Column ss:Width="330"/>
                 <Row>
                 <Cell ss:StyleID="s1"><Data ss:Type="String" >Account Name</Data></Cell>
                 <Cell ss:StyleID="s1"><Data ss:Type="String" >Next Follow-Up Date</Data></Cell>
                 <Cell ss:StyleID="s1"><Data ss:Type="String" >Next Follow-Up Action</Data></Cell>
                 <Cell ss:StyleID="s1"><Data ss:Type="String" >Primary POC</Data></Cell>
                 <Cell ss:StyleID="s1"><Data ss:Type="String" >Last DM Reached</Data></Cell>
                 </Row>
                 <apex:repeat value="{!accList}" var="a">
                 <Row>
                 <Cell><Data ss:Type="String">{!a.name}</Data></Cell>
                 <Cell><Data ss:Type="Date">{!a.NEXT_FOLLOW_UP_DATE__c}</Data></Cell>
                 <Cell><Data ss:Type="String">{!a.Next_Action_Follow_Up__c}</Data></Cell>
                 <Cell><Data ss:Type="String">{!a.Primary_POC__c}</Data></Cell>
                   <apex:repeat value="{!a.Prospecting_Activities__r}" var="t">
                 <Cell><Data ss:Type="Date">{!t.Activity_Date__c}</Data></Cell>
                     </apex:repeat>  
                 </Row>
                 </apex:repeat>
             </Table>
         </Worksheet>
    </Workbook>    
</apex:page>

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

I haven't tried it but you can try below lines of code:
 
<style>
            @page {  
                     size: A4 landscape; /* ISO/JIS A4 (210mm�297mm) */  
                     margin: 5mm; 
                  } 
</style>

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
SF_MonkeySF_Monkey
Hey! Thanks for the reply. I have tried it and it does not work.