Export.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php namespace Excel\Lib;
  2. use Dever;
  3. Dever::apply('autoload', 'excel', 'vendor');
  4. class Export extends Core
  5. {
  6. public function act($data = array(), $header = array(), $fileName = '', $sheet = 0, $sheetName = '', $return = false, $xls = false, $password = '', $save = '')
  7. {
  8. if (!$xls) {
  9. $xls = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  10. }
  11. if ($sheet > 0) {
  12. $xls->createSheet();
  13. }
  14. $act = $xls->setActiveSheetIndex($sheet);
  15. if ($sheetName) {
  16. $act->setTitle($sheetName);
  17. }
  18. $row = 1;
  19. if($header) {
  20. $i = 0;
  21. if (isset($header['top'])) {
  22. foreach($header['top'] as $v) {
  23. $act->setCellValue($this->cell[$i] . $row, $v);
  24. $act->getColumnDimension($this->cell[$i])->setWidth(30);
  25. $i++;
  26. }
  27. $row++;
  28. unset($header['top']);
  29. }
  30. $i = 0;
  31. foreach($header as $v) {
  32. $act->setCellValue($this->cell[$i] . $row, $v);
  33. $act->getColumnDimension($this->cell[$i])->setWidth(30);
  34. $i++;
  35. }
  36. $row++;
  37. }
  38. if($data) {
  39. $i = 0;
  40. $height = $max = 80;
  41. foreach($data as $v) {
  42. $j = 0;
  43. foreach($v as $cell) {
  44. //$cell = strip_tags($cell);
  45. $html = \Dever\Helper\Str::ishtml($cell);
  46. if ($html) {
  47. $wizard = new \PhpOffice\PhpSpreadsheet\Helper\Html;
  48. $cell = $wizard->toRichTextObject('<?xml encoding="UTF-8">' . $cell);
  49. }
  50. if (!$html && (strstr($cell, '.jpg') || strstr($cell, '.gif') || strstr($cell, '.png'))) {
  51. $key = ($i+$row);
  52. $value = false;
  53. if (strpos($cell, '||')) {
  54. $t = explode('||', $cell);
  55. $cell = $t[1];
  56. $value = $t[0];
  57. }
  58. $temp = explode(',', $cell);
  59. foreach ($temp as $ck => $cv) {
  60. $objDrawing[$ck] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  61. /*
  62. if (Dever::project('upload')) {
  63. $cv = str_replace('.jpg', '_t1.jpg', $cv);
  64. $cv = str_replace('.png', '_t1.png', $cv);
  65. $cv = Dever::load('upload/view')->get($cv);
  66. }
  67. $cv = Dever::local($cv);
  68. */
  69. if (!is_file($cv)) {
  70. continue;
  71. }
  72. $objDrawing[$ck]->setPath($cv);
  73. $objDrawing[$ck]->setHeight($height);
  74. //$objDrawing[$ck]->setWidth(150);
  75. $objDrawing[$ck]->setCoordinates($this->cell[$j] . ($i+$row));
  76. $objDrawing[$ck]->setOffsetX(12);
  77. if ($ck == 0) {
  78. $offsetY = 5;
  79. } else {
  80. $offsetY = $offsetY + $height + 5;
  81. }
  82. $objDrawing[$ck]->setOffsetY($offsetY);
  83. $objDrawing[$ck]->setWorksheet($act);
  84. }
  85. if ($value) {
  86. $act->setCellValue($this->cell[$j] . ($i+$row), $value);
  87. }
  88. $th = $height * count($temp);
  89. if ($th > $max) {
  90. $max = $th;
  91. }
  92. $act->getRowDimension($i+$row)->setRowHeight($max);
  93. } else {
  94. if (!$cell) {
  95. //$cell = "";
  96. }
  97. if (is_numeric($cell) && mb_strlen($cell) >= 10) {
  98. $cell .= "\t";
  99. }
  100. $act->setCellValue($this->cell[$j] . ($i+$row), $cell);
  101. $act->getStyle($this->cell[$j] . ($i+$row))->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  102. }
  103. $act->getColumnDimension($this->cell[$j])->setAutoSize(true);
  104. $act->getColumnDimension($this->cell[$j])->setWidth(30);
  105. $j++;
  106. }
  107. $i++;
  108. }
  109. }
  110. if ($header && $return) {
  111. return $xls;
  112. }
  113. if (!$fileName) {
  114. $fileName = uniqid(time(),true);
  115. }
  116. $write = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($xls, "Xlsx");
  117. if ($save) {
  118. $save = Dever::file('excel/' . $save . '.xlsx');
  119. $write->save($save);
  120. if ($password) {
  121. Dever::apply('autoload', 'excel', 'secure');
  122. $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
  123. $encrypt->input($save)->password($password)->output($save);
  124. }
  125. return $save;
  126. } else {
  127. if ($password) {
  128. $save = Dever::file('excel/' . $save . '.xlsx');
  129. $write->save($save);
  130. Dever::apply('autoload', 'excel', 'secure');
  131. $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
  132. $encrypt->input($save)->password($password)->output($save);
  133. header('Content-Type: application/octet-stream');
  134. header('Content-Transfer-Encoding: Binary');
  135. header("Content-disposition: attachment; filename=\"" . basename($save) . "\"");
  136. readfile($save);
  137. @unlink($save);
  138. } else {
  139. ob_end_clean();
  140. header('Content-Type: application/vnd.ms-excel');
  141. header('pragma:public');
  142. header("Content-Disposition:attachment;filename=$fileName.xlsx");
  143. $write->save('php://output');
  144. }
  145. }
  146. }
  147. }