What are the best practices for formatting data to avoid Excel formatting issues when exporting from PHP?

When exporting data from PHP to Excel, it is important to format the data properly to avoid formatting issues. One common issue is when Excel misinterprets data types, such as treating numbers as text. To avoid this, you can explicitly set the data type for each cell when exporting to Excel using PHP.

// Sample code to export data to Excel with proper formatting
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="exported_data.xls"');

// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set data type for each cell
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '123', PHPExcel_Cell_DataType::TYPE_NUMERIC);

// Write data to Excel file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;