Activity Forums Salesforce® Discussions How can we export selected record by checkbox in a table view?

  • Abhinav

    Member
    August 11, 2016 at 4:37 pm

    Hi Mohit,

    Here is the sample code :-

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

    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 this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos