note 107602 added to ref.dba
| From: | FranzKorntner | Date: | Tue, 21 Feb 2012 09:10:36 +0000 |
| Subject: | note 107602 added to ref.dba | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186251@lists.php.net to get a copy of this message | ||
If you need a 'download data' button that automatically fires up a spreadsheet (like
Excel), find that fputcsv() isn't working as expected, that none of the installed DBA database
engines create a spreadsheet that can be opened, and that XLS generating components are just too
heavy weight, then this might just hit the spot:
<?php
// simple table to present
$data = array(
array('col1','col2'),
array(1,2),
array(3,4)
);
// pretend content (which is XML) is XLS native
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"sheet.xls\";" );
// construct skeleton
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = $dom->preserveSpaces = true; // optional
$n = new DOMProcessingInstruction('mso-application',
'progid="Excel.Sheet"');
$dom->appendChild($n);
$workbook = $dom->appendChild(new DOMElement('Workbook'));
$workbook->setAttribute('xmlns','urn:schemas-microsoft-com:office:spreadsheet');
$workbook->setAttribute('xmlns:o','urn:schemas-microsoft-com:office:office');
$workbook->setAttribute('xmlns:x','urn:schemas-microsoft-com:office:excel');
$workbook->setAttribute('xmlns:ss','xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet');
$workbook->setAttribute('xmlns:html','http://www.w3.org/TR/REC-html40');
$styles = $workbook->appendChild(new DOMElement('Styles'));
$style = $styles->appendChild(new DOMElement('Style'));
$style->setAttribute('ss:ID','Default');
$worksheet = $workbook->appendChild(new DOMElement('Worksheet'));
$worksheet->setAttribute('ss:Name','sheet1');
$xmltable = $worksheet->appendChild(new DOMElement('Table'));
// populate with data
foreach ($data as $datarow) {
$xmlrow = $xmltable->appendChild(new DOMElement('Row'));
foreach ($datarow as $datacell) {
$xmlcell = $xmlrow->appendChild(new DOMElement('Cell'));
$xmldata = $xmlcell->appendChild(new DOMElement('Data', $datacell));
$xmldata->setAttribute('ss:Type', is_numeric($datacell) ? 'Number' :
'String');
}
}
// display and quit
echo $dom->saveXML();
?>
----
Server IP: 83.137.20.135
Probable Submitter: 83.163.6.71
----
Manual Page -- http://www.php.net/manual/en/ref.dba.php
Edit -- https://master.php.net/note/edit/107602
Del: integrated -- https://master.php.net/note/delete/107602/integrated
Del: useless -- https://master.php.net/note/delete/107602/useless
Del: bad code -- https://master.php.net/note/delete/107602/bad+code
Del: spam -- https://master.php.net/note/delete/107602/spam
Del: non-english -- https://master.php.net/note/delete/107602/non-english
Del: in docs -- https://master.php.net/note/delete/107602/in+docs
Del: other reasons-- https://master.php.net/note/delete/107602
Reject -- https://master.php.net/note/reject/107602
Search -- https://master.php.net/manage/user-notes.php