PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pudlExcel.php

https://github.com/darkain/pudl
PHP | 212 lines | 166 code | 44 blank | 2 comment | 32 complexity | e7a185529896bd95f69631ba2699166c MD5 | raw file
  1. <?php
  2. function xmlheader() {
  3. return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
  4. }
  5. function pudlExcel($result, $filename) {
  6. $zip = new ZipArchive();
  7. @unlink($filename);
  8. if ($zip->open($filename, ZIPARCHIVE::CREATE) !== true) {
  9. return 'Cannot open file: ' . $filename;
  10. }
  11. $zip->addFromString('[Content_Types].xml', xmlheader().'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/><Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/></Types>');
  12. $zip->addFromString('_rels/.rels', xmlheader().'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>');
  13. $zip->addFromString('docProps/app.xml', xmlheader().'<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>Microsoft Excel</Application><DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Worksheets</vt:lpstr></vt:variant><vt:variant><vt:i4>1</vt:i4></vt:variant></vt:vector></HeadingPairs><TitlesOfParts><vt:vector size="1" baseType="lpstr"><vt:lpstr>Catalog</vt:lpstr></vt:vector></TitlesOfParts><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>12.0000</AppVersion></Properties>');
  14. $zip->addFromString('docProps/core.xml', xmlheader().'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:creator>Darkain</dc:creator><cp:lastModifiedBy>Darkain</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">2013-04-11T16:42:30Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2013-04-11T21:25:10Z</dcterms:modified></cp:coreProperties>');
  15. $zip->addFromString('xl/_rels/workbook.xml.rels', xmlheader().'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/></Relationships>');
  16. $zip->addFromString('xl/theme/theme1.xml', xmlheader().'<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme"><a:themeElements><a:clrScheme name="Office"><a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1><a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1><a:dk2><a:srgbClr val="1F497D"/></a:dk2><a:lt2><a:srgbClr val="EEECE1"/></a:lt2><a:accent1><a:srgbClr val="4F81BD"/></a:accent1><a:accent2><a:srgbClr val="C0504D"/></a:accent2><a:accent3><a:srgbClr val="9BBB59"/></a:accent3><a:accent4><a:srgbClr val="8064A2"/></a:accent4><a:accent5><a:srgbClr val="4BACC6"/></a:accent5><a:accent6><a:srgbClr val="F79646"/></a:accent6><a:hlink><a:srgbClr val="0000FF"/></a:hlink><a:folHlink><a:srgbClr val="800080"/></a:folHlink></a:clrScheme><a:fontScheme name="Office"><a:majorFont><a:latin typeface="Cambria"/><a:ea typeface=""/><a:cs typeface=""/><a:font script="Jpan" typeface="MS Pゴシック"/><a:font script="Hang" typeface="맑은 고딕"/><a:font script="Hans" typeface="宋体"/><a:font script="Hant" typeface="新細明體"/><a:font script="Arab" typeface="Times New Roman"/><a:font script="Hebr" typeface="Times New Roman"/><a:font script="Thai" typeface="Tahoma"/><a:font script="Ethi" typeface="Nyala"/><a:font script="Beng" typeface="Vrinda"/><a:font script="Gujr" typeface="Shruti"/><a:font script="Khmr" typeface="MoolBoran"/><a:font script="Knda" typeface="Tunga"/><a:font script="Guru" typeface="Raavi"/><a:font script="Cans" typeface="Euphemia"/><a:font script="Cher" typeface="Plantagenet Cherokee"/><a:font script="Yiii" typeface="Microsoft Yi Baiti"/><a:font script="Tibt" typeface="Microsoft Himalaya"/><a:font script="Thaa" typeface="MV Boli"/><a:font script="Deva" typeface="Mangal"/><a:font script="Telu" typeface="Gautami"/><a:font script="Taml" typeface="Latha"/><a:font script="Syrc" typeface="Estrangelo Edessa"/><a:font script="Orya" typeface="Kalinga"/><a:font script="Mlym" typeface="Kartika"/><a:font script="Laoo" typeface="DokChampa"/><a:font script="Sinh" typeface="Iskoola Pota"/><a:font script="Mong" typeface="Mongolian Baiti"/><a:font script="Viet" typeface="Times New Roman"/><a:font script="Uigh" typeface="Microsoft Uighur"/></a:majorFont><a:minorFont><a:latin typeface="Calibri"/><a:ea typeface=""/><a:cs typeface=""/><a:font script="Jpan" typeface="MS Pゴシック"/><a:font script="Hang" typeface="맑은 고딕"/><a:font script="Hans" typeface="宋体"/><a:font script="Hant" typeface="新細明體"/><a:font script="Arab" typeface="Arial"/><a:font script="Hebr" typeface="Arial"/><a:font script="Thai" typeface="Tahoma"/><a:font script="Ethi" typeface="Nyala"/><a:font script="Beng" typeface="Vrinda"/><a:font script="Gujr" typeface="Shruti"/><a:font script="Khmr" typeface="DaunPenh"/><a:font script="Knda" typeface="Tunga"/><a:font script="Guru" typeface="Raavi"/><a:font script="Cans" typeface="Euphemia"/><a:font script="Cher" typeface="Plantagenet Cherokee"/><a:font script="Yiii" typeface="Microsoft Yi Baiti"/><a:font script="Tibt" typeface="Microsoft Himalaya"/><a:font script="Thaa" typeface="MV Boli"/><a:font script="Deva" typeface="Mangal"/><a:font script="Telu" typeface="Gautami"/><a:font script="Taml" typeface="Latha"/><a:font script="Syrc" typeface="Estrangelo Edessa"/><a:font script="Orya" typeface="Kalinga"/><a:font script="Mlym" typeface="Kartika"/><a:font script="Laoo" typeface="DokChampa"/><a:font script="Sinh" typeface="Iskoola Pota"/><a:font script="Mong" typeface="Mongolian Baiti"/><a:font script="Viet" typeface="Arial"/><a:font script="Uigh" typeface="Microsoft Uighur"/></a:minorFont></a:fontScheme><a:fmtScheme name="Office"><a:fillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="50000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="35000"><a:schemeClr val="phClr"><a:tint val="37000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="15000"/><a:satMod val="350000"/></a:schemeClr></a:gs></a:gsLst><a:lin ang="16200000" scaled="1"/></a:gradFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:shade val="51000"/><a:satMod val="130000"/></a:schemeClr></a:gs><a:gs pos="80000"><a:schemeClr val="phClr"><a:shade val="93000"/><a:satMod val="130000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="94000"/><a:satMod val="135000"/></a:schemeClr></a:gs></a:gsLst><a:lin ang="16200000" scaled="0"/></a:gradFill></a:fillStyleLst><a:lnStyleLst><a:ln w="9525" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"><a:shade val="95000"/><a:satMod val="105000"/></a:schemeClr></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="25400" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="38100" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln></a:lnStyleLst><a:effectStyleLst><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000" dist="20000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="38000"/></a:srgbClr></a:outerShdw></a:effectLst></a:effectStyle><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst></a:effectStyle><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst><a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0" lon="0" rev="0"/></a:camera><a:lightRig rig="threePt" dir="t"><a:rot lat="0" lon="0" rev="1200000"/></a:lightRig></a:scene3d><a:sp3d><a:bevelT w="63500" h="25400"/></a:sp3d></a:effectStyle></a:effectStyleLst><a:bgFillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="40000"/><a:satMod val="350000"/></a:schemeClr></a:gs><a:gs pos="40000"><a:schemeClr val="phClr"><a:tint val="45000"/><a:shade val="99000"/><a:satMod val="350000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="20000"/><a:satMod val="255000"/></a:schemeClr></a:gs></a:gsLst><a:path path="circle"><a:fillToRect l="50000" t="-80000" r="50000" b="180000"/></a:path></a:gradFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="80000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="30000"/><a:satMod val="200000"/></a:schemeClr></a:gs></a:gsLst><a:path path="circle"><a:fillToRect l="50000" t="50000" r="50000" b="50000"/></a:path></a:gradFill></a:bgFillStyleLst></a:fmtScheme></a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/></a:theme>');
  17. $zip->addFromString('xl/styles.xml', xmlheader().'<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts><fills count="3"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill><fill><patternFill patternType="solid"><fgColor rgb="FF00FF00"/><bgColor indexed="64"/></patternFill></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="2"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/><xf numFmtId="0" fontId="0" fillId="2" borderId="0" xfId="0" applyFill="1"/></cellXfs><cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/><colors><mruColors><color rgb="FF00FF00"/></mruColors></colors></styleSheet>');
  18. $zip->addFromString('xl/workbook.xml', xmlheader().'<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4507"/><workbookPr defaultThemeVersion="124226"/><bookViews><workbookView xWindow="0" yWindow="120" windowWidth="28755" windowHeight="13095"/></bookViews><sheets><sheet name="Catalog" sheetId="1" r:id="rId1"/></sheets><calcPr calcId="125725"/><fileRecoveryPr repairLoad="1"/></workbook>');
  19. ob_start();
  20. echo '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:B6"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><pane ySplit="1" topLeftCell="A2" activePane="bottomLeft" state="frozen"/><selection pane="bottomLeft" activeCell="A1" sqref="A1"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight="15"/><cols><col min="1" max="1" width="12" bestFit="1" customWidth="1"/></cols><sheetData>';
  21. $x = 1;
  22. $total = 0;
  23. $strings = array();
  24. $colcount = $result->fields();
  25. //EXPORT HEADERS
  26. $y = 0;
  27. $fields = $result->listFields();
  28. echo '<row r="' . $x . '" spans="1:' . $colcount . '" s="1" customFormat="1">';
  29. foreach ($fields as $key => $val) {
  30. $name = $val->name;
  31. $total++;
  32. $index = array_search($name, $strings, true);
  33. if ($index === false) {
  34. $strings[] = $name;
  35. $index = count($strings) - 1;
  36. }
  37. if ($key < 26) {
  38. $cell = chr(65 + $key) . $x;
  39. } else {
  40. $cell = chr(64 + floor($key / 26)) . chr(65 + ($key % 26)) . $x;
  41. }
  42. echo '<c r="' . $cell . '" s="1" t="s"><v>' . $index . '</v></c>';
  43. }
  44. echo '</row>';
  45. //EXPORT CELL DATA
  46. $x++;
  47. while ($data = $result->row(PUDL_NUMBER)) {
  48. echo '<row r="' . $x . '" spans="1:' . $colcount . '">';
  49. $y = 0;
  50. foreach ($data as $key => $val) {
  51. if ($key < 26) {
  52. $cell = chr(65 + $key) . $x;
  53. } else {
  54. $cell = chr(64 + floor($key / 26)) . chr(65 + ($key % 26)) . $x;
  55. }
  56. if ($val==='0' || $val==='0.0') {
  57. echo '<c r="' . $cell . '"><v>' . $val . '</v></c>';
  58. } else if (preg_match('/^[1-9][0-9]{0,12}\.?[0-9]{0,10}$/', $val)) {
  59. echo '<c r="' . $cell . '"><v>' . $val . '</v></c>';
  60. } else if (preg_match('/^[0-9]\.[0-9]{0,10}$/', $val)) {
  61. echo '<c r="' . $cell . '"><v>' . $val . '</v></c>';
  62. } else if (!empty($val)) {
  63. $total++;
  64. $index = array_search($val, $strings, true);
  65. if ($index === false) {
  66. $strings[] = $val;
  67. $index = count($strings) - 1;
  68. }
  69. echo '<c r="' . $cell . '" t="s"><v>' . $index . '</v></c>';
  70. }
  71. }
  72. echo '</row>';
  73. $x++;
  74. }
  75. echo '</sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup orientation="portrait" r:id="rId1"/></worksheet>';
  76. $zip->addFromString('xl/worksheets/sheet1.xml', xmlheader() . ob_get_clean());
  77. ob_start();
  78. echo '<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" ';
  79. echo 'count="' . $total . '" uniqueCount="' . count($strings) . '">';
  80. foreach ($strings as $key => $val) {
  81. echo '<si><t>' . htmlspecialchars($val) . '</t></si>';
  82. }
  83. echo '</sst>';
  84. $zip->addFromString('xl/sharedStrings.xml', xmlheader() . ob_get_clean());
  85. $zip->close();
  86. header('Content-Description: File Transfer');
  87. header('Content-Type: application/octet-stream');
  88. header('Content-Disposition: attachment; filename='.basename($filename));
  89. header('Content-Transfer-Encoding: binary');
  90. header('Expires: 0');
  91. header('Cache-Control: must-revalidate');
  92. header('Pragma: public');
  93. header('Content-Length: ' . filesize($filename));
  94. readfile($filename);
  95. @unlink($filename);
  96. }
  97. function pudlPHPExcel($list, $filename) {
  98. global $site;
  99. if (is_a($list, 'pudlResult')) {
  100. $list = $list->rows();
  101. }
  102. if (!is_array($list)) {
  103. return false;
  104. }
  105. define('PHPEXCEL_ROOT', '');
  106. require_once('PHPExcel/Autoloader.php');
  107. require_once('PHPExcel/PHPExcel.php');
  108. require_once('PHPExcel/Writer/Excel2007.php');
  109. $excel = new PHPExcel();
  110. $excel->setActiveSheetIndex(0);
  111. $excel->getProperties()->setCreator($site['title']);
  112. $sheet = $excel->getActiveSheet();
  113. $sheet->freezePane('A2');
  114. $excel->getActiveSheet()->setTitle('Price Sheet');
  115. $i = 0;
  116. $item = reset($list);
  117. foreach ($item as $key => &$val) {
  118. if ($i < 26) {
  119. $cell = chr(65 + $i) . '1';
  120. } else {
  121. $cell = chr(64 + floor($i / 26)) . chr(65 + ($i % 26)) . '1';
  122. }
  123. $sheet->SetCellValue($cell, $key);
  124. $style = $sheet->getStyle($cell);
  125. $style->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('0000FF00');
  126. $style->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  127. $style->getFont()->setBold(true);
  128. $i++;
  129. } unset($val);
  130. $row = 2;
  131. foreach ($list as $item) {
  132. $i = 0;
  133. foreach ($item as $key => &$val) {
  134. if ($i < 26) {
  135. $cell = chr(65 + $i) . $row;
  136. } else {
  137. $cell = chr(64 + floor($i / 26)) . chr(65 + ($i % 26)) . $row;
  138. }
  139. $sheet->SetCellValue($cell, $val);
  140. $style = $sheet->getStyle($cell);
  141. if ($key === 'price' || $key === 'list' || $key === 'cost' || $key === 'part_cost') {
  142. $style->getNumberFormat()->setFormatCode('$ #,##0.00');
  143. }
  144. if ($key === 'part_number' || $key === 'part_description' || $key === 'part_information' || $key === 'part_inactive_text') {
  145. $style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  146. }
  147. $i++;
  148. }
  149. $row++;
  150. }
  151. $item = reset($list);
  152. $i = 0;
  153. foreach ($item as $key => &$val) {
  154. if ($i < 26) {
  155. $cell = chr(65 + $i);
  156. } else {
  157. $cell = chr(64 + floor($i / 26)) . chr(65 + ($i % 26));
  158. }
  159. $sheet->getColumnDimension($cell)->setAutoSize(true);
  160. $i++;
  161. } unset($val);
  162. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  163. header('Content-Disposition: attachment;filename="' . $filename . '"');
  164. header('Cache-Control: max-age=0');
  165. $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  166. $writer->save('php://output');
  167. return true;
  168. }