function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ckellieckellie 

Visualforce page in Excel - Sorting Reversed

The below visualforce page is rendered in excel. The problem is the datalist is sorted in descending order rather than ascending order. In this code the footer with the totals are listed at the top of the table, the first line of the table. When the table is rendered in Salesforce, the records is sorted in ascending order, the same page rendered in excel sorts the table in descending order with the footer listed at the first line below the header row.

Below is the apex code
global with sharing class ProductCostSalesSheet3 {

    public decimal unit_price { get; set; }
    public decimal totalprice { get; set; }
    public decimal cost { get; set; }
    public decimal totaldiscount { get; set; }
    public String opname { get; set; }
    public decimal items { get; set; }
    public String currsign { get; set; }
    Set<String> propop = new Set<String>();
    public string sequence { get; set; }
    id oppi;
    Public final Opportunity o;
    

    public ProductCostSalesSheet3() {
        o = [select id, currencyisocode, name from Opportunity where id =: System.currentpageReference().getParameters().get('oppid')];
        system.debug('*********************000000000:'+ o); 
        oppi = o.id;  
       opname = o.name;
       If(o.currencyisocode == 'USD'){
       currsign = '$';
       }
       
       If(o.currencyisocode == 'EUR'){
       currsign = '€';
       }
       
        system.debug('*********************000000001:'+ currsign); 
        
    }

    public PageReference ExporttoExcel() {
        
        pagereference excel = new pagereference('/apex/productcostsalessheettoexcel3?oppid='+o.id);
        
        return excel;
    }


    public  List<TheWrapper> getTheWrapper(){
      
     For(Proposal_Option__c p : [select id, Opportunity_Product__c from Proposal_Option__c where Opportunity__c =: oppi]){
         If(propop.contains(p.Opportunity_Product__c)){}else{         
             propop.add(p.Opportunity_Product__c);
         }
     }
      
     List<TheWrapper> AllValues = new List<TheWrapper>();
           cost = 0;
           unit_price = 0;
           totalprice = 0;
           totaldiscount = 0;
           

       for(OpportunityLineItem oli: [select id, Quote_Item_Number__c, Product2.name, unitprice, discount, totalprice, cost_each__c, quantity, includes_Options__c from OpportunityLineItem where OpportunityId =: oppi]){
           
           
           If(propop.contains(oli.id)){           
          
           } else{
           sequence = string.valueof(oli.Quote_Item_Number__c);

           cost = cost + oli.cost_each__c;
           unit_price = unit_price + oli.unitprice;
           totalprice = totalprice + oli.totalprice;
           If( oli.discount > 0){ totaldiscount= totaldiscount+(oli.unitprice * oli.quantity * (oli.discount/100));}
           
           AllValues.add(new TheWrapper(sequence,oli.product2.name,oli.unitprice,oli.quantity, oli.cost_each__c, oli.discount,oli.totalprice,oli.includes_Options__c,oli.includes_Options__c));
          }
        
       }
       
       for(Proposal_Option__c p : [select id, Opportunity_Product__c, Description__c,quote_item_number__c, sequence__c,sales_price__c,discount__c,cost_each__c,unit_price__c,quantity__c from Proposal_Option__c where Opportunity__c =: oppi]){
           string name;
          
           If(p.cost_each__c ==null){} else{
           cost = cost + p.cost_each__c;}
           unit_price = unit_price + p.unit_price__c;
           totalprice = totalprice + p.sales_price__c;
           If( p.discount__c > 0){ totaldiscount=totaldiscount + (p.unit_price__c * p.quantity__c * (p.discount__c/100));}
           
           If( p.description__c.contains('List')){
               name = p.description__c.remove(' List Price');
           } else{
               name = 'Option: '+p.description__c;
           }
           sequence = p.quote_item_number__c +'-'+p.sequence__c;
           boolean checkbox = false;
           AllValues.add(new TheWrapper(sequence,name,p.unit_price__c,p.quantity__c,p.cost_each__c,p.discount__c,p.sales_price__c,checkbox,checkbox));


       }
           
       allValues.sort();
       
       items = allValues.size();
                 
       return AllValues ;
      

    }
}
Below is the Visualforce Page
<apex:page controller="ProductCostSalesSheet3" tabStyle="Opportunity" >
 <apex:form >
 
 <apex:pageBlock title="SE Margin Spreadsheet for {!opname}" >
     <apex:pageBlockButtons >
         <apex:commandButton action="{!ExporttoExcel}" Value="Export to Excel"/>
     </apex:pageBlockButtons>
<apex:dataTable title="SE Margin Spreadsheet for {!opname}" border="1" value="{!TheWrapper}" var="Wrapper" cellpadding="5" id="theTable" rowClasses="odd,even" styleClass="tableClass">

           <apex:column >
                <apex:facet name="header">Sort Order Number</apex:facet>
                <apex:outputText value="{!Wrapper.column1}"/>
                <apex:facet name="footer">{!items}</apex:facet>
          </apex:column>


           <apex:column >
                <apex:facet name="header">Product Name</apex:facet>
                <apex:outputText value="{!Wrapper.column2}"/>
                <apex:facet name="footer">Total</apex:facet>
          </apex:column>
          

          <apex:column style="text-align:right;" >
                <apex:facet name="header">Unit Price</apex:facet>
                <apex:outputtext value="{!if(Wrapper.column3==null,'',Currsign)}{!if(Wrapper.column8=='true','',
                                                    If(len(text(Wrapper.column3))==6, left(text(Wrapper.column3),3)&','&right(text(Wrapper.column3),3),
                                                        If(len(text(Wrapper.column3))==5, left(text(Wrapper.column3),2)&','&right(text(Wrapper.column3),3),
                                                            If(len(text(Wrapper.column3))==4, left(text(Wrapper.column3),1)&','&right(text(Wrapper.column3),3),unit_price))))}"/>
                <apex:facet name="footer">
                <apex:outputPanel id="unitPanel" style="float:right;">
                    {!Currsign}{!If(len(text(unit_price))==6, left(text(unit_price),3)&','&right(text(unit_price),3),
                                If(len(text(unit_price))==5, left(text(unit_price),2)&','&right(text(unit_price),3),
                                If(len(text(unit_price))==4, left(text(unit_price),1)&','&right(text(unit_price),3),unit_price)))}
                </apex:outputpanel></apex:facet>
          </apex:column>
          
          <apex:column style="text-align:right;" >
                <apex:facet name="header">Quantity</apex:facet>
                <apex:outputText value="{!If(right(text(Wrapper.column4),3)=='.00',left(text(Wrapper.column4), Len(text(Wrapper.column4))-3),text(Wrapper.column4))}"/>

          </apex:column>
          
          <apex:column style="text-align:right;" >
                <apex:facet name="header">Cost Each</apex:facet>
                <apex:outputText value="{!if(Wrapper.column5==null,'',Currsign)}{!If(len(text(Wrapper.column5))==6, left(text(Wrapper.column5),3)&','&right(text(Wrapper.column5),3),
                                                        If(len(text(Wrapper.column5))==5, left(text(Wrapper.column5),2)&','&right(text(Wrapper.column5),3),
                                                            If(len(text(Wrapper.column5))==4, left(text(Wrapper.column5),1)&','&right(text(Wrapper.column5),3),Wrapper.column5)))}"/>
                <apex:facet name="footer"><apex:outputPanel id="costPanel" style="float:right;">
                                    {!Currsign}{!If(len(text(round(round(cost, 2), 2)))==6, left(text(round(round(cost, 2), 2)),3)&','&right(text(round(round(cost, 2), 2)),3),
                                If(len(text(round(round(cost, 2), 2)))==5, left(text(round(round(cost, 2), 2)),2)&','&right(text(round(round(cost, 2), 2)),3),
                                If(len(text(round(round(cost, 2), 2)))==4, left(text(round(round(cost, 2), 2)),1)&','&right(text(round(round(cost, 2), 2)),3),round(round(cost, 2), 2))))}
                </apex:outputpanel></apex:facet>
          </apex:column>

          <apex:column style="text-align:right;" >
                <apex:facet name="header">Before Discount FM (%)</apex:facet>
                <apex:outputText value="{!text(round((Wrapper.column3-Wrapper.column5)/Wrapper.column3,4)*100)&if(isnull(Wrapper.column5),'','%')}"/>
                <apex:facet name="footer"><apex:outputPanel id="FMPanel" style="float:right;">
              {!text(round((unit_price-cost)/unit_price*100,2))&'%'}
                </apex:outputpanel></apex:facet>
          </apex:column>
          
          <apex:column style="text-align:right;" >
                <apex:facet name="header">Discount</apex:facet>
                <apex:outputText value="{!If(Wrapper.column6=null,'0.00',Wrapper.column6)}"/>
            <apex:facet name="footer"><apex:outputPanel id="discount" style="float:right;">
              {!round(totaldiscount/unit_price*100,2)}
                </apex:outputpanel></apex:facet>
          </apex:column>  

          <apex:column style="text-align:right;" >
                <apex:facet name="header">After Discount FM %</apex:facet>
                <apex:outputText value="{!text(round((Wrapper.column7-Wrapper.column5)/Wrapper.column7,4)*100)&If(isnull(Wrapper.column5),'','%')}"/>
                 <apex:facet name="footer"><apex:outputPanel id="AfterFMPanel" style="float:right;">
              {!text(round((totalprice-cost)/totalprice*100,2))&'%'}
                </apex:outputpanel></apex:facet>
          </apex:column>  
                                    
          <apex:column style="text-align:right;" >
                <apex:facet name="header">Total Price</apex:facet>
                <apex:outputtext value="{!if(Wrapper.column7==null,'',Currsign)}{!if(Wrapper.column8=='true','',
                                                    If(len(text(Wrapper.column7))==6, left(text(Wrapper.column7),3)&','&right(text(Wrapper.column7),3),
                                                        If(len(text(Wrapper.column7))==5, left(text(Wrapper.column7),2)&','&right(text(Wrapper.column7),3),
                                                            If(len(text(Wrapper.column7))==4, left(text(Wrapper.column7),1)&','&right(text(Wrapper.column7),3),unit_price))))}"/>
                <apex:facet name="footer"><apex:outputPanel id="datePanel" style="float:right;">
                    {!Currsign}{!If(len(text(totalprice))==6, left(text(totalprice),3)&','&right(text(totalprice),3),
                                If(len(text(totalprice))==5, left(text(totalprice),2)&','&right(text(totalprice),3),
                                If(len(text(totalprice))==4, left(text(totalprice),1)&','&right(text(totalprice),3),totalprice)))}
                </apex:outputpanel></apex:facet>
          </apex:column>

    </apex:dataTable>

    </apex:pageBlock>
    </apex:form>
</apex:page>

How do I solve the problem?

Thank you,