Sometime after exporting an excel it does have space in top of the sheet. Due to this space excel workbook content format lost.
Steps to resolve this issue on Excel and CSV
Step1 – Remove whitespace form file top or before PHP open and close tag.
Step2 – Add PHP functions ob_clean(); and flush(); after php download header.
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename='Demo_excel.xlsx');
header('Cache-Control: max-age=0');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') .' GMT');
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
ob_clean();
flush();