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
tamilselvan ctamilselvan c 

Unable to create .xlsx file from VF Page

Hi All,
Unable to create .xlsx file from VF Page. I checked in google. all are available for creating .xls file . If i change the MIME Type to work with .xlsx file  contenttype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" , it is throwing corrupted file error.
Could you please help on this.
sachinarorasfsachinarorasf
Hi Tamilselvan,

I have gone through your problem.
 
Vf page: 
<apex:page controller="ExportToExcelMultipleSheets" contentType="txt/xml#myTest.xlsx" cache="true">
    <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="1" x:FullRows="1">
  <Column ss:Width="170"/>
  <Row>
 <Cell ss:StyleID="s1"><Data ss:Type="String" >Account Name</Data></Cell>
 </Row>
  <apex:repeat value="{!accountList}" var="account">
  <Row>
 <Cell><Data ss:Type="String">{!account.name}</Data></Cell>
 </Row>
 </apex:repeat>
 </Table>
 </Worksheet>
 <Worksheet ss:Name="Contacts">
  <Table x:FullColumns="1" x:FullRows="1">
  <Column ss:Width="170"/>
  <Column ss:Width="280"/>
  <Column ss:Width="330"/>
  <Row>
  <Cell ss:StyleID="s1"><Data ss:Type="String" >Contact Name</Data></Cell>
   <Cell ss:StyleID="s1"><Data ss:Type="String" >Email</Data></Cell>
   <Cell ss:StyleID="s1"><Data ss:Type="String" >Account Name</Data></Cell>
  </Row>
  <apex:repeat value="{!contactList}" var="contact">
  <Row>
  <Cell><Data ss:Type="String">{!contact.name}</Data></Cell>
  <Cell><Data ss:Type="String">{!contact.email}</Data></Cell>
  <Cell><Data ss:Type="String">{!contact.account.name}</Data></Cell>
   </Row>
  </apex:repeat>
</Table>
 </Worksheet>
</Workbook>
</apex:page>

Apex Controller:
public with sharing class ExportToExcelMultipleSheets {
    
    public List<account> accountList{get;set;}
    public List<contact> contactList{get;set;}
    public String xlsHeader{
        get{
            String strHeader = '';
            strHeader += '<?xml version="1.0"?>';
            strHeader += '<?mso-application progid="Excel.Sheet"?>';
            return strHeader;
        }
    }
    public ExportToExcelMultipleSheets(){
        accountList = [select Name,Id from Account LIMIT 10];
        contactList = [SELECT id,Name, LastName, Account.Name, Email FROM Contact WHERE Email != '' AND Account.Name != '' LIMIT 5];
    }
    public Pagereference exportAll(){
        return new Pagereference('/apex/exportAll');
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora