title: Convert JSON Data into Excel File Download in Javascript
body: <p>I have a AJAX POST Request fetching data back in form of a JSON Array.
  I want to convert this received JSON Data into an Excel File (Not CSV) for download (on a button-click), pls help. The JSON Data might have blank values and missing fields for each JSON row. </p>
  
  <p>I tried this in Client side using Javascript but not in Java server side in which case I will have to use @Produces(MediaType.MULTIPART_FORM_DATA) in the AJAX End-Point method, which is something I can try but think its complex.</p>
  
  <p>a) AJAX Request code:</p>
  
  <pre><code>function fileUploadFunction() {
  
      var file = $('input[name="file"').get(0).files[0];
      var formData = new FormData();
  
      if(file.name != null) {
          document.getElementById("btnUpload").disabled = false;
  
          formData.append('file', file);
          $.ajax({
              url : "&lt;%=request.getContextPath()%&gt;/rest/upload/upload",
              type : "POST",
              data : formData,
              cache : false,
              contentType : false,
              processData : false,
              success : function(response) {
                  //Store result in Session and Enable Download button
                  var cacheString = JSON.stringify(response, null, 2);
                  console.log("-----------------&gt; cacheString is: " + cacheString);
                  if(cacheString != null &amp;&amp; cacheString != "[]") {
                      document.getElementById("download").disabled = false;
                  }
                  var sessionresponse = sessionStorage.setItem("i98779", cacheString); 
  
                  console.log("response is: " + response);
                  console.log("cacheString is: " + cacheString);
                  excelDownload(cacheString);
                  //createTable(response);
                  //https://stackoverflow.com/questions/47330520/how-to-export-json-object-into-excel-using-javascript-or-jquery
  
              },
  
              error : function(jqXHR, textStatus, errorThrown) {
                  console.log(errorThrown);
                  alert("Error: " + errorThrown);
              }
  
          });//ajax ends
  
      }//if ends
  
  }//Function ends
  </code></pre>
  
  <p>b) JSON Sample data received from AJAX POST Request:</p>
  
  <pre><code>[
      {
      "entityid":2,
      "firstname":"George",
      "lastname":"Bush",
      "ssn":"",
      "city":"Houston",
      "state":"TX",
      "country":"USA",
      "zipcode":""
      },
      {
      "entityid": 8,
      "firstname": "Jim",
      "lastname": "Macron",
      "ssn": "888-88-8888",
      "city": "Paris",
      "state": "NY",
      "country": "France",
      "zipcode": "T789J"
      },
      {
      "entityid": 11,
      "firstname": "Angela",
      "lastname": "Merkel",
      "city": "Saxony",
      "zipcode": ""
      },
      {
      "entityid": 7,
      "firstname": "Donald",
      "lastname": "Trump",
      "ssn": "777-77-7777",
      "city": "Washington D.C.",
      "state": "DC",
      "country": "USA",
      "zipcode": "70000"
      }
  
  ]
  </code></pre>