Export to Excel with multiple worksheets in visualforce

http://www.sfdcpoint.com/salesforce/export-excel-multiple-worksheets-visualforce/


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

Click for Demo

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
< 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
< 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: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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' );
 
 
}
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值