What are some common pitfalls to avoid when writing data from PHP to Excel?

One common pitfall to avoid when writing data from PHP to Excel is not properly handling special characters or formatting. To ensure that data is correctly displayed in Excel, it is important to properly encode special characters and format the cells accordingly.

// Example code snippet to properly encode special characters and format cells when writing data to Excel

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

// Set encoding for special characters
$objPHPExcel->getActiveSheet()->setTitle('Sheet1');
$objPHPExcel->getActiveSheet()->setCellValue('A1', htmlspecialchars('Special Character: &'));

// Format cell as text
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

// Save the Excel file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('output.xls');