+ Start a Discussion

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">

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

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

             <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 }"/>



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"

             <Style ss:ID="s1">
             <Font ss:Bold="1"/>

         <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"/>
                 <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>
                 <apex:repeat value="{!accList}" var="a">
                 <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>

Khan AnasKhan Anas (Salesforce Developers) 

Greetings to you!

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

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
Hey! Thanks for the reply. I have tried it and it does not work.