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
force shahidforce shahid 

Headers & 1st row & 1st column empty in csv file while downloading from vfpage

Hi,
I am trying to download csv file from vf page. But it doesn't download properly.
1st row & 1st column are empty and date format is also chnaged. If field doesn't have value then next field value populate there. How can i  solve this ?

Thanks in advance

My VF apge:
<apex:page controller="OrderReport_Class" contentType="application/octet-stream#{!fileName}.csv"  cache="true" >
     Distributorcode,OrderNumber,EffectiveDate,Location,Profit
    <apex:repeat value="{!orditem}" var="item" >
        {!item.SBCF_Distributor_Code__c},{!item.order.OrderNumber},{!item.order.EffectiveDate},{!item.SBCF_Location__c},{!item.order.SBCF_ProfitCenter__c}
    </apex:repeat>
</apex:page>

Apex class :
public class OrderReport_Class
{
    public list<Order> ord{set;get;}
    public list<Orderitem> orditem{set;get;}
    public date startdate{set;get;}
    public date enddate{set;get;}
    public date enddate1{set;get;}
    public string filename{set;get;}
    public string CSVTab{set;get;}
    public string selectedorder{set;get;}
    public list<string> selectedorderitem{set;get;}
    public  List<SelectOption> oilist {set;get;}
    
    public OrderReport_Class()
    {
        startdate = system.today();
        enddate = system.today();
        enddate1 = system.today()+1;
        filename = 'OrderReport';
        oilist = new List<SelectOption>();
        CSVTab = 'OSO';
        system.debug('startdate:'+startdate);
        system.debug('enddate:'+enddate);
        orditem = [select SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBCF_Factory__c,order.EffectiveDate,order.SBCF_DiscountperlineItem__c,order.OrderNumber,order.SBCF_Tax_Code__c,order.SBCF_SpecAgencyCode__c ,order.SBCF_Terms_Code__c,order.SBCF_ShipVia__c,order.SBCF_FOB__c,order.SBCF_Freight__c,order.SBCF_PO_Number__c,order.SBCF_Estimated_Ship_Date__c,order.SBCF_JobNo__c,order.SBCF_Shipping_Instructions__c,order.AccountId,order.SBCF_BillToAttention__c,order.Billingcity,
                        order.BillingStateCode,order.BillingCountryCode,order.BillingPostalCode,order.ShipTo__c,order.SBCF_ShippingAttention__c,order.Shippingcity,order.ShippingStateCode,order.ShippingCountryCode,order.ShippingPostalCode,order.SBCF_Contact__c,order.SBCF_Account_No__c,order.SBCF_Phone__c,order.SBCF_Fax__c,order.SBCF_Email__c,order.OpportunityId,order.SBCF_SalesAgencyCode__c,order.SBCF_Comm_Split__c,order.SBCF_SpecComm__c
                        ,order.SBCF_SpecOvg8020__c,order.TotalAmount,order.SBCF_TotalOverage__c,order.SBCF_Unused1__c,order.SBCF_Ship_Date__c,order.SBQQ__Quote__c,order.SBCF_FreightAllow__c,order.SBCF_SpecOvg100__c,order.SBCF_SalesOvg100__c,order.SBCF_Unused2__c,order.SBCF_Unused3__c,order.SBCF_CSalesRepCode__c
                        ,order.SBCF_Unused4__c,order.SBCF_SalesRepComm__c,order.SBCF_Parts_Svc__c,order.SBCF_SalesOvg8020__c,order.SBCF_Use_Alt_Code__c,order.SBCF_Use_UPC_Code__c,order.SBCF_OrderedBy__c,order.SBCF_ProfitCenter__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1];
           getactivatedorders();
        
    }
     public List<SelectOption> getactivatedorders () {
        List<SelectOption> options = new List<SelectOption>();
         list<order> ord =[select id,OrderNumber from Order where ActivatedDate>=:startdate and ActivatedDate <:enddate1 and status ='Activated'];
         options.add(new selectoption('None','None'));  
         for(order dc : ord)
        {
               options.add(new selectoption(dc.OrderNumber,dc.OrderNumber));
        }
       system.debug('options:'+options);
        return options;
    }
    Public pagereference orderitemlist() // Custom Picklist values for User Field based on Designation starts here
    {
        system.debug('In method');
        oilist.clear();
        oilist.add(new selectoption('All','All'));
           list<orderitem> oitemlist = [select order.OrderNumber,id,OrderItemNumber from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 and order.OrderNumber =:selectedorder];
        system.debug('OrderNumber:'+selectedorder);
        system.debug('oitemlist:'+oitemlist);
        for(orderitem o :oitemlist)
        {
                oilist.add(new selectoption(o.OrderItemNumber,o.OrderItemNumber));
        }
        system.debug('Order Items list:'+oilist);
        oidata();
        return null;
    }
    
    public void showdata()
    {
       // orditem = [select OrderItemNumber,SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBQQ__TaxCode__c,SBCF_Factory__c,SBCF_TotalOverage__c,
        //           order.OrderNumber,order.EffectiveDate,order.SBCF_DiscountperlineItem__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 ];
            orditem = [select SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBCF_Factory__c,order.EffectiveDate,order.SBCF_DiscountperlineItem__c,order.OrderNumber,order.SBCF_Tax_Code__c,order.SBCF_SpecAgencyCode__c ,order.SBCF_Terms_Code__c,order.SBCF_ShipVia__c,order.SBCF_FOB__c,order.SBCF_Freight__c,order.SBCF_PO_Number__c,order.SBCF_Estimated_Ship_Date__c,order.SBCF_JobNo__c,order.SBCF_Shipping_Instructions__c,order.AccountId,order.SBCF_BillToAttention__c,order.Billingcity,
                        order.BillingStateCode,order.BillingCountryCode,order.BillingPostalCode,order.ShipTo__c,order.SBCF_ShippingAttention__c,order.Shippingcity,order.ShippingStateCode,order.ShippingCountryCode,order.ShippingPostalCode,order.SBCF_Contact__c,order.SBCF_Account_No__c,order.SBCF_Phone__c,order.SBCF_Fax__c,order.SBCF_Email__c,order.OpportunityId,order.SBCF_SalesAgencyCode__c,order.SBCF_Comm_Split__c,order.SBCF_SpecComm__c
                        ,order.SBCF_SpecOvg8020__c,order.TotalAmount,order.SBCF_TotalOverage__c,order.SBCF_Unused1__c,order.SBCF_Ship_Date__c,order.SBQQ__Quote__c,order.SBCF_FreightAllow__c,order.SBCF_SpecOvg100__c,order.SBCF_SalesOvg100__c,order.SBCF_Unused2__c,order.SBCF_Unused3__c,order.SBCF_CSalesRepCode__c
                        ,order.SBCF_Unused4__c,order.SBCF_SalesRepComm__c,order.SBCF_Parts_Svc__c,order.SBCF_SalesOvg8020__c,order.SBCF_Use_Alt_Code__c,order.SBCF_Use_UPC_Code__c,order.SBCF_OrderedBy__c,order.SBCF_ProfitCenter__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 ];
        system.debug('Orditems:'+orditem.size());
        
    }
    public void oidata()
    {
        if(selectedorderitem.contains('All')==True)
        {
            system.debug('selectedorderitem:'+selectedorderitem);
            //orditem = [select OrderItemNumber,SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBQQ__TaxCode__c,SBCF_Factory__c,SBCF_TotalOverage__c,
                      // order.OrderNumber,order.EffectiveDate,order.SBCF_DiscountperlineItem__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 and order.OrderNumber=:selectedorder];
            orditem = [select SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBCF_Factory__c,order.EffectiveDate,order.SBCF_DiscountperlineItem__c,order.OrderNumber,order.SBCF_Tax_Code__c,order.SBCF_SpecAgencyCode__c ,order.SBCF_Terms_Code__c,order.SBCF_ShipVia__c,order.SBCF_FOB__c,order.SBCF_Freight__c,order.SBCF_PO_Number__c,order.SBCF_Estimated_Ship_Date__c,order.SBCF_JobNo__c,order.SBCF_Shipping_Instructions__c,order.AccountId,order.SBCF_BillToAttention__c,order.Billingcity,
                        order.BillingStateCode,order.BillingCountryCode,order.BillingPostalCode,order.ShipTo__c,order.SBCF_ShippingAttention__c,order.Shippingcity,order.ShippingStateCode,order.ShippingCountryCode,order.ShippingPostalCode,order.SBCF_Contact__c,order.SBCF_Account_No__c,order.SBCF_Phone__c,order.SBCF_Fax__c,order.SBCF_Email__c,order.OpportunityId,order.SBCF_SalesAgencyCode__c,order.SBCF_Comm_Split__c,order.SBCF_SpecComm__c
                        ,order.SBCF_SpecOvg8020__c,order.TotalAmount,order.SBCF_TotalOverage__c,order.SBCF_Unused1__c,order.SBCF_Ship_Date__c,order.SBQQ__Quote__c,order.SBCF_FreightAllow__c,order.SBCF_SpecOvg100__c,order.SBCF_SalesOvg100__c,order.SBCF_Unused2__c,order.SBCF_Unused3__c,order.SBCF_CSalesRepCode__c
                        ,order.SBCF_Unused4__c,order.SBCF_SalesRepComm__c,order.SBCF_Parts_Svc__c,order.SBCF_SalesOvg8020__c,order.SBCF_Use_Alt_Code__c,order.SBCF_Use_UPC_Code__c,order.SBCF_OrderedBy__c,order.SBCF_ProfitCenter__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 and order.OrderNumber=:selectedorder];
            system.debug('Orditems:'+orditem.size());            
        }else
        {
            //orditem = [select OrderItemNumber,SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBQQ__TaxCode__c,SBCF_Factory__c,SBCF_TotalOverage__c,
                      // order.OrderNumber,order.EffectiveDate,order.SBCF_DiscountperlineItem__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 and OrderItemNumber IN:selectedorderitem];
            orditem = [select SBCF_Distributor_Code__c,SBCF_Location__c,SBCF_Fixture_Family__c,SBCF_FixtureCode__c,SBQQ__OrderedQuantity__c,UnitPrice,SBCF_Factory__c,order.EffectiveDate,order.SBCF_DiscountperlineItem__c,order.OrderNumber,order.SBCF_Tax_Code__c,order.SBCF_SpecAgencyCode__c ,order.SBCF_Terms_Code__c,order.SBCF_ShipVia__c,order.SBCF_FOB__c,order.SBCF_Freight__c,order.SBCF_PO_Number__c,order.SBCF_Estimated_Ship_Date__c,order.SBCF_JobNo__c,order.SBCF_Shipping_Instructions__c,order.AccountId,order.SBCF_BillToAttention__c,order.Billingcity,
                        order.BillingStateCode,order.BillingCountryCode,order.BillingPostalCode,order.ShipTo__c,order.SBCF_ShippingAttention__c,order.Shippingcity,order.ShippingStateCode,order.ShippingCountryCode,order.ShippingPostalCode,order.SBCF_Contact__c,order.SBCF_Account_No__c,order.SBCF_Phone__c,order.SBCF_Fax__c,order.SBCF_Email__c,order.OpportunityId,order.SBCF_SalesAgencyCode__c,order.SBCF_Comm_Split__c,order.SBCF_SpecComm__c
                        ,order.SBCF_SpecOvg8020__c,order.TotalAmount,order.SBCF_TotalOverage__c,order.SBCF_Unused1__c,order.SBCF_Ship_Date__c,order.SBQQ__Quote__c,order.SBCF_FreightAllow__c,order.SBCF_SpecOvg100__c,order.SBCF_SalesOvg100__c,order.SBCF_Unused2__c,order.SBCF_Unused3__c,order.SBCF_CSalesRepCode__c
                        ,order.SBCF_Unused4__c,order.SBCF_SalesRepComm__c,order.SBCF_Parts_Svc__c,order.SBCF_SalesOvg8020__c,order.SBCF_Use_Alt_Code__c,order.SBCF_Use_UPC_Code__c,order.SBCF_OrderedBy__c,order.SBCF_ProfitCenter__c from orderitem where order.status = 'Activated' and order.ActivatedDate>=:startdate and order.ActivatedDate <:enddate1 and OrderItemNumber IN:selectedorderitem];
            system.debug('Orditems:'+orditem.size());
        }
       
    }
    
}