1

I want to use javascript, to export the html table to excel. I used below script. Its working fine. Since few cells have special characters, I have escaped them. However, rows and contents after this special characters cell are not downloading. Please help. Here is the code:

<script type="text/javascript">
            $(document).ready(function(){      
    $("#exportToExcel").click(function() {  
        var data='<table border="1" class="csstable">'+$("#myTable").html().replace(/^[a-zA-Z!”$%&’()*\+,\/;\[\\\]\^_`{|}~<>]+$/gi, '')+'</table>';
                $('body').prepend("<form method='post' action='exporttoexcel.php' style='display:none' id='ReportTableData'><input type='text' name='tableData' value='"+data+"' ></form>");
         $('#ReportTableData').submit().remove();
         return false;
    });

});
</script>



<?php  
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".date('d-m-Y').'-'.date("H:i:s").'-'."myfile.xls");
header("Content-Transfer-Encoding: binary ");

echo strip_tags($_POST['tableData'],'<table><th><tr><td>');  
?>
  • maybe escaping is not enough.... how about doing encodeURIComponent on the values instead? – Steen Jun 07 '13 at 11:41
  • Are escape characters syntax is correct? I'm poor at regex. I tried to use URI,but no data will be downloaded. But excel sheet will be populated. – user2463267 Jun 07 '13 at 12:00
  • I found the error. Its with Regex. If I'm using single quote ('), then only its giving problem. Rest every characters downloaded. Plz help to escape this single quote. I tried a lot, but not working. – user2463267 Jun 07 '13 at 18:33

1 Answers1

0

Think this is what you want: http://jsfiddle.net/9zaH7/

  1. takes table html, wraps in new table - this part seems not to be needed, but maybe you need to remove the styling/class?

  2. escapes the complete table to the form text field.

    $(document).ready(function(){
    $("#exportToExcel").click(function() {

        var data='<table border="1" class="csstable">'+$("#myTable").html()+'</table>';
        data=escape(data);
                $('body').prepend("<form method='post' action='exporttoexcel.php' style='display:block' id='ReportTableData'><input type='text' name='tableData' value='"+data+"'></form>");
    
         return false;
    });});
    
Steen
  • 2,749
  • 2
  • 20
  • 36
  • This problem is fixed, since I escaped the single quote charater with double quote. This works fine for certian amount of data. If I want to export more than 10000 rows, then its giving error inside the downloaded excel file.Please help, how to get larger data. – user2463267 Jul 01 '13 at 17:59
  • probably you need to set a maxRequestLength in web.config.... or whats relevant for your system (don't know php) - maybe this article? http://stackoverflow.com/questions/2364840/what-is-the-size-limit-of-a-post-request – Steen Jul 02 '13 at 10:54
  • Thanks Steen.. I'll check and post the status. Keeping finger crossed ;) – user2463267 Jul 02 '13 at 17:47