Skip to content

Instantly share code, notes, and snippets.

@thiagosf
Created May 27, 2011 11:03
Show Gist options
  • Save thiagosf/995040 to your computer and use it in GitHub Desktop.
Save thiagosf/995040 to your computer and use it in GitHub Desktop.
Helper para exportar dados para Excel
<?php
class ExcelHelper extends AppHelper
{
var $filename = 'arquive.csv';
var $rows = array();
var $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
var $footer = "</Workbook>";
var $worksheet_title = "Table";
function getHeaders () {
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $this->filename . ".xls\"");
}
function addRow ($data = array()) {
foreach($data as $key => $value) {
$data[$key] = "<Cell><Data ss:Type=\"String\">" . $value . "</Data></Cell>\n";
}
$this->rows[] = $data;
}
function setTitle ($title) {
$title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
$title = substr ($title, 0, 31);
$this->worksheet_title = $title;
}
function render ($file = null) {
$this->filename = ($file) ? $file : $this->filename;
$this->getHeaders();
$out = array();
foreach($this->rows as $row) {
$out[] = "<Row>\n" . implode('', $row) . "</Row>\n";
}
$data = implode("\n", $out);
echo stripslashes ($this->header);
echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n";
echo $data;
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
echo $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment