Thursday, December 5, 2013

Generate formatted Excel sheet in Salesforce

Sample:

Using Microsoft Office XML formats we can generate formatted excel sheet. For more detail refer below sites. Using schema provided by office, we can create formatted word document also.

http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx

Page : FormattedExcel


<apex:page controller="ExcelXMLController" contentType="application/vnd.ms-excel#XYZ.xls">
    {!ExcelData}
    <c:excelgenerator></c:excelgenerator>
    {!EOF}
</apex:page>


Class : ExcelXMLController 





public class ExcelXMLController {

    public String getExcelData() {
        return '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';
    }
    public String getEOF() {
        return '</Workbook>';
    }

}


Component : ExcelGenerator


<apex:component controller="AccountData">
    <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">
     <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
      <Author>Ankurkumar Amratlal Patel</Author>
      <LastAuthor>Ankurkumar Amratlal Patel</LastAuthor>
      <Created>2013-12-06T05:12:53Z</Created>
      <Company>HP</Company>
      <Version>14.00</Version>
     </DocumentProperties>
     <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
      <WindowHeight>10035</WindowHeight>
      <WindowWidth>22995</WindowWidth>
      <WindowTopX>480</WindowTopX>
      <WindowTopY>120</WindowTopY>
      <ProtectStructure>False</ProtectStructure>
      <ProtectWindows>False</ProtectWindows>
     </ExcelWorkbook>
     <Styles>
      <Style ss:ID="Default" ss:Name="Normal">
       <Alignment ss:Vertical="Bottom"/>
       <Borders/>
       <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
       <Interior/>
       <NumberFormat/>
       <Protection/>
      </Style>
      <Style ss:ID="s70">
       <Alignment ss:Horizontal="Center" ss:WrapText="1" ss:Vertical="Bottom"/>
       <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#FF0000"
        ss:Bold="1"/>
      </Style>
     </Styles>
     <Worksheet ss:Name="Sheet1">
      <Names>
       <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=Sheet1!R1C1:R1C3"
        ss:Hidden="1"/>
      </Names>
      <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="{!excelRowCnt}" x:FullColumns="1"
       x:FullRows="1" ss:DefaultRowHeight="15">
       <Column ss:Index="2" ss:Width="102"/>
       <Row>
        <Cell ss:StyleID="s70"><Data ss:Type="String">Account Name</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
        <Cell ss:StyleID="s70"><Data ss:Type="String">Type</Data><NamedCell ss:Name="_FilterDatabase"/></Cell>
        <Cell ss:StyleID="s70"><Data ss:Type="String">SLA</Data><NamedCell
          ss:Name="_FilterDatabase"/></Cell>
       </Row>
       <apex:repeat value="{!accList}" var="acc">
           <Row>
            <Cell><Data ss:Type="String">{!acc.Name}</Data></Cell>
            <Cell><Data ss:Type="String">{!acc.Type}</Data></Cell>
            <Cell><Data ss:Type="String">{!acc.SLA__c}</Data></Cell>
           </Row>
       </apex:repeat>
      </Table>
      <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
       <PageSetup>
        <Header x:Margin="0.3"/>
        <Footer x:Margin="0.3"/>
        <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
       </PageSetup>
       <Print>
        <ValidPrinterInfo/>
        <HorizontalResolution>600</HorizontalResolution>
        <VerticalResolution>600</VerticalResolution>
       </Print>
       <Selected/>
       <Panes>
        <Pane>
         <Number>3</Number>
         <ActiveRow>7</ActiveRow>
         <ActiveCol>9</ActiveCol>
        </Pane>
       </Panes>
       <ProtectObjects>False</ProtectObjects>
       <ProtectScenarios>False</ProtectScenarios>
      </WorksheetOptions>
      <AutoFilter x:Range="R1C1:R1C3" xmlns="urn:schemas-microsoft-com:office:excel">
      </AutoFilter>
     </Worksheet>
    </Workbook>
</apex:component>


Class : AccountData



public class AccountData{
    public List<Account> accList{get;set;}
    public Integer excelRowCnt {get;set;}
    public AccountData(){
        accList = [Select Name,Type,SLA__c from Account];
        excelRowCnt = 4 + accList.size();
    }
}

2 comments:

  1. Thank you for very useful information.
    If you add little explanations where required, will be an additional help.

    ReplyDelete