Problems with the mozilla browser

2

I have a problem; I have a method to generate an excel from a table and it works but in Google Chrome, however I will try in mozilla and it does not work ...

Any suggestions?

CODE

<title>Reportes Equipo</title>
<meta charset="utf-8">
    <!--Librerias para Exportar excel--> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="JS/exportarExcel.js" type="text/javascript"></script>

      <!--Librerias para datepicker-->            
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
      <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
      <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
      <script src="JS/datepicker.js" type="text/javascript"></script>

The structure of the table is basic; And with this botton I create the excel.

 <input type='submit'  value="EXPORTAR" style='background:url("IMG/excel.png") no-repeat; padding-left: 30px ; height: 30px' id="btnExport" />  

Finally, this is the part of what creates the excel.

$(document).ready(function() {
     $("#btnExport").click(function(e) {

         e.preventDefault();

         // calculamos la fecha actual 
          var hoy = new Date();
          dia = hoy.getDate(); 
          mes = hoy.getMonth() + 1 ;
          anio= hoy.getFullYear();
          fecha_actual = String(dia + "_" + mes + "_" + anio);

         // Pagina que hizo el llamado 
        var pagina = document.getElementById('pagina').value;

         //Obtenemos los valores de la tabla 
         var data_type = 'data:application/vnd.ms-excel';
         var table_div = document.getElementById('table_wrapper');
         var table_html = table_div.outerHTML.replace(/ /g, '%20');

         var a = document.createElement('a');
         a.href = data_type + ', ' + table_html;
         a.download = 'Reporte ' + pagina + ' '+ fecha_actual + '.xls';
         a.click();
     });
});

It does not generate any errors, just do not create the excel.

Any solution?

    
asked by Andres Felipe Diaz 17.05.2017 в 23:40
source

2 answers

2

After reading a lot in different forums I did not find a solution to my problem, apparently mozilla-Firefox is not compatible, so it is necessary to use another function like this:

 window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#table_wrapper').html()));
          e.preventDefault(); 

The problem with this solution is that the file name is static and when using encodeURIComponent the name of the file will be incomprehensible.

However the information is very well conserved (ALWAYS YOU DO NOT USE SPECIAL CHARACTERS-> IS MY CASE)

Unfortunately I did not find any more solutions than to place the 2 methods and recommend the use of Google Chroom to make reports.

My code stayed something like this:

$(document).ready(function() {
     $("#btnExport").click(function(e) {
        var es_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

        if (es_firefox ) {
          window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#table_wrapper').html()));
          e.preventDefault();
        }else{
         var a = document.createElement('a');  

         // calculamos la fecha actual 
          var hoy = new Date();
          dia = hoy.getDate(); 
          mes = hoy.getMonth() + 1 ;
          anio= hoy.getFullYear();
          fecha_actual = String(dia + "_" + mes + "_" + anio);

         // Pagina que hizo el llamado 
        var pagina = document.getElementById('pagina').value;

         //Obtenemos los valores de la tabla 
         var data_type = 'data:application/vnd.ms-excel';
         var table_div = document.getElementById('table_wrapper');
         var table_html = table_div.outerHTML.replace(/ /g, '%20');


         a.href = data_type + ', ' + table_html;
         a.download = 'Reporte ' + pagina + ' '+ fecha_actual + '.xls';
         a.click();

         e.preventDefault();  
        }

     });
});

What I do is that it is valid that the browser is using and I use the necessary function ..

I hope to help someone with my case (and)

    
answered by 18.05.2017 / 16:34
source
1

The problem is that a.click() does not start the download in Mozilla. You should put the newly created a on the page with a legend that says "excel generated, download here" or something like that.

It also has to replace

var table_html = table_div.outerHTML.replace(/ /g, '%20');

for

var table_html = table_div.innerHTML.replace(/ /g, '%20');
    
answered by 18.05.2017 в 01:16