What are the potential pitfalls when exporting data to Excel using PHP scripts?

One potential pitfall when exporting data to Excel using PHP scripts is that special characters or formatting may not be handled correctly, leading to data corruption or loss of information. To solve this issue, it is important to properly encode the data before writing it to the Excel file using functions like `htmlentities()` or `htmlspecialchars()`.

// Encode data before exporting to Excel
$data = array(
    array('Name', 'Age', 'Email'),
    array('John Doe', 30, 'john.doe@example.com'),
    array('Jane Smith', 25, 'jane.smith@example.com')
);

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

// Set data to the active sheet
$objPHPExcel->getActiveSheet()->fromArray($data, NULL, 'A1');

// Encode special characters
foreach ($objPHPExcel->getActiveSheet()->getRowIterator() as $row) {
    foreach ($row->getCellIterator() as $cell) {
        $cell->setValue(htmlentities($cell->getValue(), ENT_COMPAT, 'UTF-8'));
    }
}

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