What are the recommended headers to send when creating Excel files in PHP to avoid displaying random characters in the output?

When creating Excel files in PHP, it is important to set the appropriate headers to ensure that the output is displayed correctly in Excel. Without the proper headers, Excel may display random characters or prompt the user to open the file in a different format. To avoid this issue, it is recommended to set the headers for Excel files by specifying the content type as "application/vnd.ms-excel" and adding a Content-Disposition header to prompt the browser to download the file as an attachment.

<?php
// Set headers for Excel file download
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="excel_file.xls"');

// Create Excel file content
// Add your code here to generate Excel content

// Output the Excel file content
echo "Your Excel file content here";
?>