Mail Merge after exporting to XLS Excel5 (Mac Office 2011)

Topics: Developer Forum
Nov 4, 2013 at 7:10 AM
Hi there,

I have recently added PHPExcel to a clients site and it's great.

The only issue is when the XLS file is downloaded and he tries to create a mail merge in Office 2011 (Mac). When he tries to use the XLS document, he gets an error.


The only way he can get it to work is open the XLS document and Save As an XLSX file.

I gave it a try on my Mac which also has Office 2011 and I get a different error.


I manage to get it to work by first opening the XLS document and hitting Save. It's like the formatting of the document isn't quite right and I have to save again for Excel to fix it.

Here is my code:
// This function is used to clean the data in the cells
function clean_export_cell($string) {
    $string = stripslashes($string);
    $string = trim($string);
    $string = str_replace(''', "'", $string);
    $string = str_replace('&', "&", $string);
    return $string;

// Required files

// Start PHPExcel object
$objPHPExcel = new PHPExcel();

// Set font

// Set page orientation

// Set page size

// Default cell width and row height

// Heading
$row = 1;
$col = 0;
foreach($field_names AS $field_name) {
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, clean_export_cell($field_name));

// Data
if (!empty($csv_array)) {
    foreach($csv_array AS $csv_array_data) {
        $col = 0;
        foreach($csv_array_data AS $csv_array_data_value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, clean_export_cell($csv_array_data_value));

header('Content-Encoding: UTF-8');
header('Content-Type: application/; charset=UTF-8');
header('Content-Disposition: attachment; charset=UTF-8; filename="sample.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
Is there an issue with my code or is this a Office 2011 issue?