<a href="purchase_full_report.php?init=true">Download in Excel</a>
<?php
function ExportExcel()
{
    // code
}

if($_GET['init'])
{
    ExportExcel();    
}
?>
<script type="text/javascript">
$('#toexcel').live("click",function() {
            $.ajax({
                url: "toExcel.php",
                data: "sql="+encodeURIComponent(sql),
                beforeSend: function(){
                    $("#wait").show();
                },
                complete: function(){
                    $("#wait").hide();
                },
                success: function(response){
                    window.location.href = response.url;
                }
            });
        });

</script>