How do I eliminate the Excel warning popup when exporting a JSON array using JavaScript?

I export a JSON array to Excel using JavaScript. However, Excel shows a warning popup on open. How can I prevent this? Revised code example follows:

function exportToSpreadsheet(dataArray) {
  const headerLine = Object.keys(dataArray[0]).join(',') + '\n';
  const dataLines = dataArray.map(item => Object.values(item).join(',')).join('\n');
  const fileContent = headerLine + dataLines;
  
  const fileBlob = new Blob([fileContent], { type: 'application/vnd.ms-excel' });
  const downloadElem = document.createElement('a');
  downloadElem.href = URL.createObjectURL(fileBlob);
  downloadElem.download = 'ExportedFile.xlsx';
  downloadElem.click();
}

The warning popped up for me because Excel was seeing a mismatch between the file extension and the content format. When I attempted to export CSV data as an .xlsx file, Excel correctly flagged it as a potential issue. I fixed it by either changing the file extension to .csv and updating the MIME type accordingly or by using a library that correctly generates an actual Excel file. In my experience, aligning the file’s content with its extension is crucial to avoid these confusing warnings.