ExcelHelper.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace General\Helpers;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. /**
  6. * excel 工具类
  7. */
  8. class ExcelHelper
  9. {
  10. /**
  11. * web端导出csv文件
  12. * @param array $column_names 列名
  13. * @param array $data 导出数据内容
  14. * @param string $filename 导出文件名
  15. */
  16. public static function exportWebFileCsv(array $column_names, array $data, string $filename)
  17. {
  18. header("Content-type:application/vnd.ms-excel");
  19. header("Content-Disposition:attachment;filename=" . $filename . ".csv");
  20. $column_names = collect($column_names)->map(function ($item) {
  21. return "\"" . mb_convert_encoding($item, "GBK", "UTF-8") . "\"";
  22. })->all();
  23. echo implode(",", $column_names);
  24. echo "\r\n";
  25. foreach ($data as $item) {
  26. $rows = collect($item)->map(function ($row) {
  27. return "\"" . mb_convert_encoding(is_numeric($row) && strlen($row) > 10 ? "'" . $row : $row, "GBK", "UTF-8") . "\"";
  28. })->all();
  29. echo implode(",", $rows);
  30. echo "\r\n";
  31. }
  32. exit();
  33. }
  34. /**
  35. * 导出csv到服务器
  36. */
  37. public static function saveFileCsv(array $column_names, array $data, string $file_name, string $path)
  38. {
  39. $file_path = $path . '/' . $file_name;
  40. if (file_exists($file_path)) {
  41. unlink($file_path);
  42. }
  43. $fp = fopen($file_path, "a");
  44. $column_names = collect($column_names)->map(function ($item) {
  45. return mb_convert_encoding($item, "GBK", "UTF-8");
  46. })->all();
  47. fputcsv($fp, $column_names);
  48. $limit = 1000;
  49. $calc = 1;
  50. foreach ($data as $item) {
  51. $calc++;
  52. if ($limit == $calc) {
  53. ob_flush();
  54. flush();
  55. $calc = 0;
  56. }
  57. $rows = collect($item)->map(function ($row) {
  58. return mb_convert_encoding(is_numeric($row) && strlen($row) > 10 ? "'" . $row : $row, "GBK", "UTF-8");
  59. })->all();
  60. fputcsv($fp, $rows);
  61. }
  62. fclose($fp);
  63. }
  64. protected $excel_service;
  65. public function __construct()
  66. {
  67. $this->excel_service = new Spreadsheet();
  68. }
  69. /**
  70. * 保存Excel格式数据表 需要composer require phpoffice/phpspreadsheet
  71. * @param string $title
  72. * @param array $header
  73. * @param array $data
  74. * @param string $file_name
  75. * @param string $path
  76. */
  77. public function setExcelData(string $title, array $header, array $data, int $sheet_index = 0)
  78. {
  79. if ($sheet_index > 0) {
  80. $this->excel_service->createSheet($sheet_index);
  81. }
  82. $this->excel_service->setActiveSheetIndex($sheet_index);
  83. $sheet = $this->excel_service->getActiveSheet();
  84. $sheet->setTitle($title);
  85. $column_index = 1;
  86. foreach ($header as $value) {
  87. $sheet->getColumnDimensionByColumn($column_index)->setAutoSize(true);
  88. $sheet->setCellValueByColumnAndRow($column_index, 1, $value);
  89. $column_index++;
  90. }
  91. $row = 2;
  92. foreach ($data as $val) {
  93. $column_index = 1;
  94. foreach ($val as $item) {
  95. $sheet->setCellValueByColumnAndRow($column_index, $row, is_numeric($item) && strlen($item) > 10 ? "'" . $item : $item);
  96. $column_index++;
  97. }
  98. $row++;
  99. }
  100. }
  101. public function saveExcelFile(string $file_name, string $path)
  102. {
  103. if (!$path) {
  104. // Redirect output to a client’s web browser (Xlsx)
  105. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  106. header('Content-Disposition: attachment;filename=' . $file_name . ".xlsx");
  107. header('Cache-Control: max-age=0');
  108. // If you're serving to IE 9, then the following may be needed
  109. header('Cache-Control: max-age=1');
  110. // If you're serving to IE over SSL, then the following may be needed
  111. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  112. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  113. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  114. header('Pragma: public'); // HTTP/1.0
  115. $writer = IOFactory::createWriter($this->excel_service, 'Xlsx');
  116. $writer->save('php://output');
  117. //return true;
  118. } else {
  119. $writer = IOFactory::createWriter($this->excel_service, 'Xlsx');
  120. $writer->save($path . '/' . $file_name);
  121. }
  122. }
  123. public function saveExcelData(array $header, array $data, string $file_name, string $path)
  124. {
  125. $this->setExcelData(substr($file_name, 0, 30), $header, $data);
  126. $this->saveExcelFile($file_name, $path);
  127. }
  128. public function saveExcelDataToMultiSheet(array $param, string $file_name, string $path = '')
  129. {
  130. foreach ($param as $item) {
  131. $this->setExcelData($item['title'], $item['header'], $item['data']);
  132. }
  133. $this->saveExcelFile($file_name, $path);
  134. }
  135. }