ExcelHelper.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. class ExcelHelper
  5. {
  6. /**
  7. * @param string $pFilename
  8. * @param bool $skipFirstRow
  9. * @return array|bool
  10. */
  11. public static function load($pFilename, $skipFirstRow = true)
  12. {
  13. if (!is_file($pFilename)) {
  14. return false;
  15. }
  16. $data = [];
  17. $objPHPExcel = \PHPExcel_IOFactory::load($pFilename);
  18. foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  19. foreach ($worksheet->getRowIterator() as $row) {
  20. if ($row->getRowIndex() == 1 && $skipFirstRow) {
  21. continue;
  22. }
  23. $newRow = [];
  24. /** @var \PHPExcel_Worksheet_RowCellIterator $cellIterator */
  25. $cellIterator = $row->getCellIterator();
  26. $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
  27. foreach ($cellIterator as $cell) {
  28. $newRow[] = $cell == null ? null : $cell->getCalculatedValue();
  29. }
  30. $data[] = $newRow;
  31. }
  32. }
  33. return $data;
  34. }
  35. /**
  36. * @param array $source
  37. * @param array $header
  38. * @param string $attachmentName
  39. * @throws \Exception
  40. */
  41. public static function output($source, $header = [], $attachmentName)
  42. {
  43. $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
  44. if (!\PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
  45. throw new \Exception($cacheMethod . " caching method is not available");
  46. }
  47. \PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyDefaultValueBinder());
  48. // Create new PHPExcel object
  49. $objPHPExcel = new \PHPExcel();
  50. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  51. $objPHPExcel->setActiveSheetIndex(0);
  52. // Add some data
  53. if ($header) {
  54. $objPHPExcel->getActiveSheet()->fromArray($header, null, 'A1', true);
  55. $objPHPExcel->getActiveSheet()->fromArray($source, null, 'A2', true);
  56. } else {
  57. $objPHPExcel->getActiveSheet()->fromArray($source, null, 'A1', true);
  58. }
  59. // Redirect output to a client’s web browser (Excel2007)
  60. // Yii::$app->getResponse()->setDownloadHeaders($attachmentName)->send();
  61. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  62. // $objWriter->save('php://output');
  63. $saveDir = Yii::getAlias('@webroot') . '/excel/';
  64. if (!is_dir($saveDir)) {
  65. mkdir($saveDir, 0755, true);
  66. }
  67. $saveFile = $saveDir . $attachmentName . '_' . mt_rand(10000, 99999);
  68. $objWriter->save($saveFile);
  69. Yii::$app->getResponse()->sendFile($saveFile, $attachmentName, ['mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'])->send();
  70. @unlink($saveFile);
  71. Yii::$app->end();
  72. }
  73. public static function init()
  74. {
  75. set_time_limit(0);
  76. ini_set('memory_limit', '1024M');
  77. }
  78. }