Export to Excel with multiple worksheets in visualforce
We need to use XML tags to get the excel with multiple worksheets. We can use visualforce tags also with XML tags. We can use Styles like color , size , height for formatting the cells of excel sheet. For XML spreadsheet reference, go to
XML Spreadsheet Reference
Below is the Visualforce page code that will list all Accounts and Contacts. There is a button that will export all these accounts and contacts to excel sheet with two worksheets.
<apex:page controller="ExportToExcelMultipleSheets"> <apex:form > <apex:pageBlock title="Accounts and Contacts"> <apex:pageBlockButtons > <apex:commandbutton value="Export All Accounts and Contacts" action="{!exportAll}"/> </apex:pageBlockButtons> <apex:pageBlockSection columns="2"> <apex:pageBlockSectionItem > <apex:pageBlockTable title="All Accounts" value="{!accountList}" var="account"> <apex:facet name="caption" ><b>All Accounts</b></apex:facet> <apex:column value="{!account.name}"/> </apex:pageBlockTable> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:pageBlockTable title="All Contacts" value="{!contactList}" var="contact"> <apex:facet name="caption" ><b>All Contacts</b></apex:facet> <apex:column value="{!contact.name}"/> <apex:column value="{!contact.email}"/> <apex:column value="{!contact.account.name}"/> </apex:pageBlockTable> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Below is the Visualforce page code that will generate excel file.
<apex:page controller="ExportToExcelMultipleSheets" contentType="txt/xml#myTest.xls" 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>
Below controller is used by both the visualforce pages:
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 id, name from Account LIMIT 50]; contactList = [Select id, name, account.name, email from Contact LIMIT 50]; } public Pagereference exportAll(){ return new Pagereference('/apex/exportAll'); } }
30 comments
2 pings
Skip to comment form
I am getting both accounts and contacts in one sheet only. i copied the same code. Please help me.!
Hello
Can you please try running ‘Click for demo’ link on this page and check if this code is generating Account and Contacts in one sheet or multiple sheets?
Thanks for ur reply,!
I’ve checked ‘Click for demo’ link and the link you posted above both are working fine. But i dont know why it is not working for me. Here i am posting the link please check it once.
http://myprivate-developer-edition.ap1.force.com/
strHeader += ”;
strHeader += ”;
Remove comment in above to the following and it’s work fine.
strHeader += ”;
strHeader += ”;
Please check the below code :
Visual force page to display Accounts and Contacts :
All Accounts
All Contacts
Visual force page to download Accounts and Contacts :
Account Name
{!account.name}
Contact Name
Email
Account Name
{!contact.name}
{!contact.email}
{!contact.account.name}
Apex Class :
public with sharing class ExportToExcelMultipleSheets {
public List accountList{get;set;}
public List contactList{get;set;}
public String xlsHeader {
get {
String strHeader = ”;
strHeader += ‘‘;
strHeader += ‘‘;
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’);
}
}
Thanks!
This is link
http://demopoint-developer-edition.ap1.force.com/apex/ExportToExcelWithMultipleWorkSheets
Both the link are working fine, but mine is not working. Please check it once http://myprivate-developer-edition.ap1.force.com/
Thanks!
Hello,
Can you please try copy-paste our given code as it is because there seems some difference in our code and yours.
Below is your chunk of code where it seems the difference:
I guess , xmlns:v=”urn:schemas-microsoft-com:vml” and part is causing the issue, as you have also used the same reference that is in other parts of the code. So please try copy paste the same provided code and let us know, if it still doesn’t work for you.
Thanks
Hi, Now i just copied the above code provided in this site and pasted as it is. Now i am getting like this. http://myprivate-developer-edition.ap1.force.com/ .
Thanks !
Now i got it ! There was a small mistake i didn’t notice. The xml code is in comments.
strHeader += ‘‘;
strHeader += ‘‘;
I removed the code in comments.
”;
”;
Thanks !
Can u please say what exactly u missed out !!
can u post test class ..i cant cover.
.
public String xlsHeader {
get {
String strHeader = ”;
strHeader += ‘‘;
strHeader += ‘‘;
return strHeader;
}
}
public static testmethod void ExportToExcelMultipleSheets(){
ExportToExcelMultipleSheets EE = new ExportToExcelMultipleSheets();
EE.exportAll();
system.debug(‘*****EE.xlsHeader **********’+EE.xlsHeader);
}
Hello
I am using the same code ! But it is not coming in 2 excel sheets .
Hi Ankush,
I am trying to create a excel file using your code above. When i check the demo link it is working fine and excel is coming in two worksheets, however when i am generating the excel by the code it is giving a excel with a single worksheet and two rows with all Accounts and Contacts values.
I think there is something different in the exportAll page just the way it is in the controller where you have just written List not List or List.
Can you or any one who has figure this out please help me and other people here.
Thanks!
Can anyone please post the right code to this functionality nothing is working for me. Please post the actual content present in that “Demo Link” shared. The code pasted here here is not working at all.
Thanks,
Kumaresan M
Hello,
i’ve tried the same code but its not working for me, i’ve tried to compare text content of the demo file and my file i found this part is present in mine and absent in the demo file :
SfdcApp.Visualforce.viewstate.ViewstateSender.sendViewstate(‘https://eu5.salesforce.com’, ‘VF004_ExportCSVFile’);
Does anyone have the same issue ? how can i remove that part before the file generation ?
Thank you all
I am having that same issue. I can not figure out how it is working in the Demo.
I got error when i tryed to open the downloaded file of open the file generated as resul : “General Error.
General input/output error.” for libre office . need any other configuration ?
Hey Can you tell me if we can freeze the headers as well
Approved
That is really important for me
Approved
Can someone tell me how to utilize this code but filter each workbook separately? Have separate results for each workbook but use the same object to pull from?
Thanks
Author
You can have different data in tag. If its single object, then create separate list in apex and use it in apex:repeat as per your requirement.
I am using the same code! But it is not coming in 2 excel sheets.
& I get an error as File corrupt and cannot be opened
Can you help me out
Is there any way to merge the cells in excel based on the group by query?
I used this code and when i click export button the excel file is downloaded but it is showing file is corrupted when opening it..
try to view the file using online viewer of xml file.
by using this code, we will get data in a single file but 2sheets
and file corrupt error is showing so try to view the file using online xml viewer.
[…] to Excel with multiple worksheets in visualforce – Salesforce Tips & Tricks. Export to Excel with multiple worksheets in visualforce – Salesforce Tips & Tricks. Visualforce – How to ensure that all of the cell content shows in an Excel contenttype page. I am […]
[…] Export to Excel with multiple worksheets in visualforce – Salesforce Tips & Tricks. Export to Excel with multiple worksheets in visualforce – Salesforce Tips & Tricks. Visualforce – How to ensure that all of the cell content shows in an Excel contenttype page. I am exporting a page as Excel page as described here (using contenttype). I am unsure how to ensure that the contents of the cells are showing. After opening the file, I could change it manually, by setting the cell height to Auto-fit and setting the cell contents to wrap, but I would like to do this automatically. I am guessing there must be a property that can be set in style which is explicitly for Excel which will define these two properties. […]