1

I create a visualforce (to build excel file) and controller to saved it in Salesforce (ContentVersion). The generated file format is .xls but when I view the file it shows as xml syntax.

Visualforce (to build excel file)

<apex:page id="pg" standardStylesheets="false" controller="KoinWorksApplicationController" 
    contenttype="application/vnd.ms-excel#KoinWorksApplicationSheet.xls">
{!xmlheader}
<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></Author>
      <LastAuthor></LastAuthor>
      <Company></Company>
      <Version>1</Version>
    </DocumentProperties>

    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
      <WindowHeight>16000</WindowHeight>
      <WindowWidth>20480</WindowWidth>
      <WindowTopX>400</WindowTopX>
      <WindowTopY>400</WindowTopY>
      <ProtectStructure>False</ProtectStructure>
      <ProtectWindows>False</ProtectWindows>
    </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s67">
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Double" ss:Weight="3"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:Size="14"/>
   <Interior ss:Color="#FFF58C" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s68">
   <Alignment ss:Vertical="Bottom"/>
   <Font ss:Size="12" ss:Bold="1"/>
   <Interior ss:Color="#FFCC00" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s70">
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Double" ss:Weight="3"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:Size="14"/>
   <Interior ss:Color="#FFF58C" ss:Pattern="Solid"/>
   <NumberFormat ss:Format="Percent"/>
  </Style>
  <Style ss:ID="s72">
   <Alignment ss:Vertical="Top"/>
   <Borders/>
   <Font/>
  </Style>
  <Style ss:ID="s75">
   <Alignment ss:Vertical="Top"/>
   <NumberFormat ss:Format="0.0%"/>
  </Style>
 </Styles>
 <Names>
  <NamedRange ss:Name="Amount_Column" ss:RefersTo="=Applications!R2C3:R{!appsSize + 1}C3"/>
  <NamedRange ss:Name="Name_Column" ss:RefersTo="=Applications!R2C1:R{!appsSize + 1}C1"/>
  <NamedRange ss:Name="Percentage_of_Total_Column" ss:RefersTo="=Applications!R2C4:R{!appsSize + 1}C4"/>
  <NamedRange ss:Name="Stage_Column" ss:RefersTo="=Applications!R2C2:R{!appsSize + 1}C2"/>
  <NamedRange ss:Name="Total_Amount_Cell" ss:RefersTo="=Applications!R{!appsSize + 2}C3"/>
 </Names>
 <Worksheet ss:Name="Applications">
  <Table ss:ExpandedColumnCount="60" ss:ExpandedRowCount="5" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="100">
   <Row ss:Height="16">
    <Cell ss:StyleID="s68"><Data ss:Type="String">Application Id</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Bank</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Nama Program</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Jumlah Pinjaman</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Jangka Waktu</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Tujuan Pinjaman</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Nama Lengkap</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">KTP</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">NPWP</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Jenis Kelamin</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Tempat Lahir</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Tanggal Lahir</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Ibu Kandung</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">No Telepon</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">No Telepon Lainnya</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Email</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Status Kawin</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Nama Lengkap</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Tempat Lahir</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Tanggal Lahir</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Ibu Kandung</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">No Telepon</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Status Kerabat</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Alamat</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kode Pos</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RT</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RW</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Provinsi</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kabupaten / Kota</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kecamatan</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kepemilikan</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Lama Menetap</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Alamat</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kode Pos</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RT</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RW</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Provinsi</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kabupaten / Kota</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kecamatan</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kepemilikan</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Lama Menetap</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Apa pekerjaan Anda?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Dimana Tempat Anda Bekerja?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Berapa Penghasilan Anda Setiap Bulan?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Alamat</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kode Pos</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RT</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">RW</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Provinsi</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kabupaten / Kota</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kecamatan</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Pinjaman apa saja yang Anda miliki saat ini?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Berapa Total Jumlah Pinjamannya?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Berapa Total cicilan Pinjaman Tersebut Tiap Bulannya?</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Rekening bank 1</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Rekening bank 2</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Rekening bank 3</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">KTP</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Foto Aplikan dengan KTP</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">Kartu Keluarga</Data></Cell>
 </Row>
   <apex:repeat value="{!apps}" var="app">
   <Row>
    <Cell ><Data ss:Type="String">{!app.Name}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Partner__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Program_Name__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!TEXT(app.Refinancing_Amount__c)}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="Number">{!app.Refinancing_Term__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Refinancing_Purpose__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Name__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Number__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.NPWP__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Gender__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Birthplace__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Birthdate_Formula__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Mother_Maiden_Name__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Primary_Phone__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Secondary_Phone__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Email__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Marital_Status__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Name__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Birthplace__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Birthdate_Formula__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Mother_Maiden_Name__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Mobile__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Reference_Relationship__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_ZIP_Code__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_RT__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_RW__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_Province__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_City__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Address_Kecamatan__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.House_Ownership_ID__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Staying_Period_ID_Year__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_ZIP_Code__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_RT__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_RW__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_Province__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_City__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Domicile_Address_Kecamatan__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.House_Ownership_Domicile__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Staying_Period_Domicile_Year__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Job__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Name__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Monthly_Salary__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_ZIP_Code__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_RT__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_RW__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_Province__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_City__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Company_Address_Kecamatan__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Current_Debt__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Total_Debt_Amount__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Monthly_Installment__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Account_Number_1_Formula__c}</Data><NamedCell ss:Name="Name_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Account_Number_2_Formula__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Account_Number_3_Formula__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.ID_Link__c}</Data><NamedCell ss:Name="Stage_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Applicant_with_ID_Photo_Link__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
    <Cell ><Data ss:Type="String">{!app.Family_Certificate_Photo_Link__c}</Data><NamedCell ss:Name="Amount_Column"/></Cell>
   </Row>
   </apex:repeat>
  </Table>
 </Worksheet>
</Workbook>

</apex:page>

Controller

public class KoinWorksApplicationController {
    public List<Refinancing_Application__c> apps {get;set;}
    public Integer appsSize{get;set;}
    public String applicationId {get;set;}

    public String xmlheader {get;set;}
    public String endfile{get;set;}


    public KoinWorksApplicationController(){
        xmlheader ='<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';
        endfile = '</Workbook>';
        applicationId = ApexPages.currentPage().getParameters().get('id');
        apps = [SELECT Name, Account_Number_1__c,Account_Number_1_Bank__c,Account_Number_1_PP__c, Account_Number_1_Formula__c, Current_Debt__c, 
                           Account_Number_2__c,Account_Number_2_Bank__c,Account_Number_2_PP__c, Account_Number_2_Formula__c,
                           Account_Number_3__c,Account_Number_3_Bank__c,Account_Number_3_PP__c, Account_Number_3_Formula__c, Additional_Guarantee__c, 
                           Birthdate__c, Birthplace__c, Birthdate_Formula__c, Company_Address__c, Company_Address_Province__c, Company_Address_City__c, 
                           Company_Address_ZIP_Code__c, Company_Address_Kecamatan__c, Company_Address_RT__c, Company_Address_RW__c, Company_Extension__c, Company_Fax__c, Company_Name__c,
                           Company_Phone__c, Contact__c, Correspondence_Address__c, Down_Payment__c, Domicile_Address__c, Domicile_Address_Province__c, Domicile_Address_City__c, 
                           Domicile_Address_ZIP_Code__c, Domicile_Address_Kecamatan__c, Domicile_Address_RT__c, Domicile_Address_RW__c, Education_Other__c, 
                           Effective_Rate__c, Email__c, Flat_Rate__c, Gender__c, Grade__c,
                           Guarantor_Birthdate__c, Guarantor_Birthplace__c, Guarantor_Full_Name__c, Guarantor_Gender__c, Guarantor_ID_Expired_Date__c,
                           Guarantor_ID_Number__c, Guarantor_Job__c, Guarantor_Job_Other__c, Guarantor_Mobile__c, Hobby__c,
                           House_Ownership_ID__c, House_Ownership_ID_Other__c, House_Ownership_Domicile__c, House_Ownership_Domicile_Other__c, ID_Address__c, ID_Address_Province__c, 
                           ID_Address_City__c, ID_Address_ZIP_Code__c, ID_Address_Kecamatan__c, ID_Address_RT__c, ID_Address_RW__c, ID_Expired_Date__c, ID_Number__c, 
                           Installment_Payment_Method__c, Installment_Payment_Method_Other__c, Insurance_Coverage__c, Insurance_Type__c, Insurance_Type_Other__c,
                           Interview_Location__c, Interview_Time__c, Job__c, Joint_Income__c, Lead_Source_Name__c, Lead_Source_Smartfinance__c,
                           Main_Income__c, Main_Income_Method__c, Marital_Status__c, Monthly_Installment__c, Monthly_Salary__c, Mother_Maiden_Name__c,
                           Name__c, Nationality__c, NPWP__c, Other_Condition__c, Partner__c, PP__c, Premium_Payment_Amount__c, Premium_Payment_Method__c,
                           Primary_Phone__c, Program_Name__c, Promotion_Media__c, Reference_Address__c, Reference_Birthdate__c, Reference_Birthplace__c, 
                           Reference_Gender__c, Reference_Mobile__c, Reference_Mother_Maiden_Name__c, Reference_Name__c, Reference_Phone__c, Reference_Birthdate_Formula__c,
                           Reference_Relationship__c, Refinancing_Amount__c, Refinancing_Purpose__c, Refinancing_Term__c, Religion__c, Religion_Other__c,
                           Secondary_Phone__c, Seller_Address__c, Seller_Name__c, Seller_Phone__c, Source_of_Additional_Income__c, Source_of_Additional_Income_Other__c, 
                           Source_of_Income__c, Source_of_Income_Other__c, Spouse_Income__c, Status__c, Staying_Period_ID_Month__c, Staying_Period_ID_Year__c, Staying_Period_Domicile_Month__c, Staying_Period_Domicile_Year__c,
                           Total_Debt_Amount__c, Total_Revenue__c, Total_Revenue_Other__c, Vehicle_BPKB_PP__c, Vehicle_Brand__c, Vehicle_Name_in_BPKB__c, 
                           Vehicle_Police_Number__c, Vehicle_Price__c, Vehicle_Production_Year__c, Vehicle_Type__c, Vehicle_Type_Other__c, Vehicle_Usage__c, ID_Link__c, Applicant_with_ID_Photo_Link__c, Family_Certificate_Photo_Link__c
                           FROM Refinancing_Application__c WHERE Id=:applicationId LIMIT 1];
        appsSize = apps.size();
    }
}

Controller2 (to call Page and saved as xls in salesforce)

public class RefinancingDataEntry {
    public Contact con{get;set;}
    public List<Bank_Product__c> listBank{get;set;}
    public String contactId{get;set;}
    public String partnerName{get;set;}
    Blob body;
    private List<Refinancing_Application__c> lastApplication{get;set;}
    public Refinancing_Application__c newApplication{get;set;}
    String currentDebt ='';
    Decimal totalDebt=0;

    public RefinancingDataEntry(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        partnerName = Apexpages.currentPage().getUrl();
        if(partnerName.contains('KoinWorks')){
            partnerName = 'KoinWorks';
        }
        else if(partnerName.contains('Smartfinance')){
            partnerName = 'Smartfinance';
        }
        con = [SELECT Id, FirstName, Name, LastName, Gender__c, ID_Number__c, ID_Card_Expired_Date__c, BirthDate, Place_of_Birth__c,
               Religion__c, Other_Religion__c, Hobby__c, Mother_s_Maiden_Name__c, Marital_Status__c, Last_Education_1__c, 
               Nationality__c, ID_Address__c, Current_Address__c, 
               Phone, MobilePhone, HomePhone, Fax, Email, Account_Number__c, NPWP_Number__c, Bank_Name__c, P_P__c 
               FROM Contact WHERE Id=:contactId];

        if(con.id!=null){
            listBank = [SELECT Product__r.Name, Outstanding_Amount__c FROM Bank_Product__c WHERE Contact__c=:contactId];
            for(Bank_Product__c bp : listBank){
                currentDebt += bp.Product__r.Name + ', ';
                totalDebt += bp.Outstanding_Amount__c;
            }
        }

        lastApplication = [SELECT Account_Number_1__c,Account_Number_1_Bank__c,Account_Number_1_PP__c, Account_Number_1_Formula__c,
                           Account_Number_2__c,Account_Number_2_Bank__c,Account_Number_2_PP__c, Account_Number_2_Formula__c,
                           Account_Number_3__c,Account_Number_3_Bank__c,Account_Number_3_PP__c, Account_Number_3_Formula__c, Additional_Guarantee__c, 
                           Birthdate__c, Birthplace__c, Company_Address__c, Company_Extension__c, Company_Fax__c, Company_Name__c,
                           Company_Phone__c, Contact__c, Correspondence_Address__c, Down_Payment__c, Domicile_Address__c, Domicile_Address_Province__c, Domicile_Address_City__c, 
                           Domicile_Address_ZIP_Code__c, Domicile_Address_Kelurahan__c, Domicile_Address_Kecamatan__c, Domicile_Address_RT__c, Domicile_Address_RW__c, Education__c, Education_Other__c, 
                           Effective_Rate__c, Email__c, Fax__c, Flat_Rate__c, Gender__c, Grade__c,
                           Guarantor_Birthdate__c, Guarantor_Birthplace__c, Guarantor_Full_Name__c, Guarantor_Gender__c, Guarantor_ID_Expired_Date__c,
                           Guarantor_ID_Number__c, Guarantor_Job__c, Guarantor_Job_Other__c, Guarantor_Mobile__c, Hobby__c, House_Ownership_Domicile_Formula__c, House_Ownership_ID_Formula__c,
                           House_Ownership_ID__c, House_Ownership_ID_Other__c, House_Ownership_Domicile__c, House_Ownership_Domicile_Other__c, ID_Address__c, ID_Address_Province__c, 
                           ID_Address_City__c, ID_Address_ZIP_Code__c, ID_Address_Kelurahan__c, ID_Address_Kecamatan__c, ID_Address_RT__c, ID_Address_RW__c, ID_Expired_Date__c, ID_Number__c, 
                           Installment_Payment_Method__c, Installment_Payment_Method_Other__c, Insurance_Coverage__c, Insurance_Type__c, Insurance_Type_Other__c,
                           Interview_Location__c, Interview_Time__c, Job__c, Joint_Income__c, Lead_Source_Name__c, Lead_Source_Smartfinance__c,
                           Main_Income__c, Main_Income_Method__c, Marital_Status__c, Monthly_Installment__c, Mother_Maiden_Name__c,
                           Name__c, Nationality__c, NPWP__c, Other_Condition__c, Partner__c, PP__c, Premium_Payment_Amount__c, Premium_Payment_Method__c, Provision_Fee__c,
                           Primary_Phone__c, Program_Name__c, Promotion_Media__c, Reference_Address__c, Reference_Birthdate__c, Reference_Birthplace__c, 
                           Reference_Gender__c, Reference_Mobile__c, Reference_Mother_Maiden_Name__c, Reference_Name__c, Reference_Phone__c, 
                           Reference_Relationship__c, Refinancing_Amount__c, Refinancing_Purpose__c, Refinancing_Term__c, Religion__c, Religion_Other__c,
                           Secondary_Phone__c, Seller_Address__c, Seller_Name__c, Seller_Phone__c, Source_of_Additional_Income__c, Source_of_Additional_Income_Other__c, 
                           Source_of_Income__c, Source_of_Income_Other__c, Spouse_Income__c, Status__c, Staying_Period_ID_Month__c, Staying_Period_ID_Year__c, Staying_Period_Domicile_Month__c, Staying_Period_Domicile_Year__c,
                           Total_Debt_Amount__c, Total_Revenue__c, Total_Revenue_Other__c, Vehicle_BPKB_PP__c, Vehicle_Brand__c, Vehicle_Name_in_BPKB__c, Interested_in_Refinancing_Info__c,
                           Vehicle_Police_Number__c, Vehicle_Price__c, Vehicle_Production_Year__c, Vehicle_Type__c, Vehicle_Type_Other__c, Vehicle_Usage__c,Guarantor_Address__c, Guarantor_Phone__c, Company_Email__c, Job_Other__c,
                           Additional_Income__c
                           FROM Refinancing_Application__c WHERE Contact__c =: contactId ORDER BY CreatedDate DESC LIMIT 1];

            newApplication = new Refinancing_Application__c();
            newApplication.Name__c = con.Name;
            newApplication.Gender__c = con.Gender__c;
            newApplication.Birthdate__c = con.Birthdate;
            newApplication.Birthplace__c = con.Place_of_Birth__c;
            newApplication.ID_Number__c = con.ID_Number__c;
            newApplication.Contact__c = con.id;
            newApplication.Partner__c = partnerName;
            newApplication.ID_Expired_Date__c = con.ID_Card_Expired_Date__c;
            newApplication.Primary_Phone__c = con.Phone;
            newApplication.Secondary_Phone__c = con.MobilePhone;
            newApplication.Email__c = con.Email;
            newApplication.NPWP__c = con.NPWP_Number__c;
            newApplication.ID_Address__c = con.ID_Address__c;
            newApplication.Domicile_Address__c = con.Current_Address__c;
            newApplication.Account_Number_1__c = con.Account_Number__c;
            newApplication.Account_Number_1_Bank__c = con.Bank_Name__c;
            newApplication.Current_Debt__c = currentDebt;
            newApplication.Total_Debt_Amount__c = totalDebt;
    }

    public String GenerateKoinWorksExcel(){
        insert newApplication;
        PageReference pdf = Page.KoinWorksApplicationSheet;
        pdf.getParameters().put('id',newApplication.Id);
        body = pdf.getContent();     

        try{            
            ContentVersion v = new ContentVersion();
            v.versionData = body;
            v.title = 'KoinWorksApplication '+newApplication.Name__c;
            v.pathOnClient ='/test.xls'; //set the dummy path on client attribute (required)
            insert v;

            List<ContentVersion> ContentVersions = [select ContentDocumentID from ContentVersion where id =: v.id];                
            //if content document ids are present
            if(!ContentVersions.isEmpty())
            {
                //share the content with related contact
                ContentDocumentLink contentDocumentLinkObj = new ContentDocumentLink();
                contentDocumentLinkObj.ContentDocumentId = ContentVersions.get(0).ContentDocumentID;
                contentDocumentLinkObj.Visibility = 'AllUsers'; 
                contentDocumentLinkObj.LinkedEntityId = contactId;
                contentDocumentLinkObj.ShareType='V';
                insert contentDocumentLinkObj;

                List<ContentWorkspace> CWList = [select id from ContentWorkspace where name = 'Generated Documents'];
                if(!CWList.isEmpty())
                {
                    ContentWorkspaceDoc CWD = new ContentWorkspaceDoc(ContentWorkspaceId = CWList.get(0).id, ContentDocumentId = ContentVersions.get(0).ContentDocumentID);
                    insert CWD;

                    List<ContentWorkspaceDoc>  CWDList = [SELECT ID FROM ContentWorkspaceDoc WHERE ID =: CWD.id];
                    if(!CWDList.isEmpty()){
                        return 'Success';
                    }
                }
            }
        }
        catch(Exception e){
            return e.getMessage();
        }
        return null;        
    }
}

Here is the image of generated xls enter image description here

  • Does the file open properly in excel? – Kasper Dec 14 '17 at 12:50
  • Is there any specific reason you are using the Workbook, style, row, cell tags? Is there any new way of building the excel file? If it is so, ignore this comment if not you could simple build and HTML table with tr and td tags and it will work for sure. – Shailesh Patil Dec 14 '17 at 12:52
  • @kaper it doesn't open properly. Get an error 'file is corrupt". – Febrian Tarigan Dec 14 '17 at 14:13
  • @ShaileshPatil i just following guide from somebody blog post. I will try it. Thanks for your input! – Febrian Tarigan Dec 14 '17 at 14:13
  • this might help - salesforce.stackexchange.com/questions/120935/… - related issue but (I wrote that answer) was how I got .xls files using xml to open as expected – cropredy Dec 17 '17 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.