title: Download maatwebsite excel using ajax in laravel
body: <p>I'm trying to download an excel file using ajax method in laravel.
  Controller function:</p>
  
  <pre><code>$myFile = Excel::create($name, function ($excel) use ($export) {
              $excel-&gt;sheet('Data', function ($sheet) use ($export) {
                  $sheet-&gt;fromArray($export);
  
                  $sheet-&gt;cells('A1:N1', function ($cells) {
  
                      $cells-&gt;setBackground('#dbdbdb');
                      $cells-&gt;setFontColor('#000000');
                      $cells-&gt;setFontWeight('bold');
                      $cells-&gt;setFont(array(
                          'family' =&gt; 'Calibri',
                          'size'   =&gt; '9',
  
                      ));
  
                  });
  
                  $sheet-&gt;setStyle(array(
                      'font' =&gt; array(
                          'name' =&gt; 'Calibri',
                          'size' =&gt; 9,
  
                      ),
                  ));
  
              });
          });
          $myFile   = $myFile-&gt;string('xlsx'); 
          $response = array(
              'name' =&gt; $name, 
              'file' =&gt; "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," . base64_encode($myFile), 
          );
  
          return response()-&gt;json($response);
  </code></pre>
  
  <p>Ajax function:</p>
  
  <pre><code>$(document).on('click', '.ExportJobs', function() {
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
      var ids = [];
      $(".InvoiceCheckBox:checked").each(function(e) {
          ids.push(this.value);
      });
      data = {
          "ids": ids,
      };
      $.ajax({
          method: "POST",
          url: "/exportNew",
          data: data,
          success: function(response) {
              var a = document.createElement("a");
              a.href = response.file;
              a.download = response.name;
              document.body.appendChild(a);
              a.click();
              a.remove();
          }
      });
  });
  </code></pre>
  
  <p>But using above controller method is not returning excel formatted file if I change string value from <code>xlsx</code> to <code>csv</code> then <code>csv</code> formatted file is getting downloaded.</p>
  
  <p>How do we make the excel formatted file downloaded? Any suggestions, Please!</p>