And now for your Excel export code, given a random array stored in $array.
<?php
// Set filename for download
$fileName = 'my_excel_export';
if (count($array) == 0) {
throw new Zend_Exception('Sorry, there is no data available to
export.');
}
else {
// Initiate data variable that will hold th data for the entire
worksheet
$data = '';
// Set column names.
// Let's say we have a name and e-mail filed that we wish to export
$headers = "name\temail\t";
foreach ($array as $row) {
// New row in worksheet
$line = '';
foreach ($row as $cellValue) {
// New cell in row
if ((! isset($cellValue)) or ($cellValue == "")) {
// This cell is empty
$cellValue = "\t";
}
else {
// This cell has a value
$cellValue = str_replace('"', '""', $cellValue); // Replace
double quotes, or they will break your csv
$cellValue = '"' . $cellValue . '"' . "\t"; // Wrap entire
value in double quotes
// You may have to decode from UTF-8 to ISO, Excel cannot
deal with UTF-8
// $value = utf8_decode($value);
}
// Add cell to row
$line .= $cellValue;
}
$data .= trim($line) . "\n";
}
// Strip out carriage returns
$data = str_replace("\r", "", $data);
// Set Excel headers
header('Content-type: application/x-msdownload');
header('Content-Disposition: attachment; filename='.$fileName.'.xls');
// Echo output
echo $headers . "\n" . $data;
}
?>
--
View this message in context:
http://www.nabble.com/Create-an-Excel-file-from-the-returned-table-rows-tp24474784p24478282.html
Sent from the Zend Framework mailing list archive at Nabble.com.