ListsExcelTrait.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\lists;
  15. use app\common\cache\ExportCache;
  16. use PhpOffice\PhpSpreadsheet\IOFactory;
  17. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  18. use PhpOffice\PhpSpreadsheet\Style\Border;
  19. use PhpOffice\PhpSpreadsheet\Style\Fill;
  20. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  21. trait ListsExcelTrait
  22. {
  23. public int $pageStart = 1; //导出开始页码
  24. public int $pageEnd = 200; //导出介绍页码
  25. public string $fileName = ''; //文件名称
  26. /**
  27. * @notes 创建excel
  28. * @param $excelFields
  29. * @param $lists
  30. * @return string
  31. * @throws \PhpOffice\PhpSpreadsheet\Exception
  32. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  33. * @author 令狐冲
  34. * @date 2021/7/21 16:04
  35. */
  36. public function createExcel($excelFields, $lists)
  37. {
  38. $title = array_values($excelFields);
  39. $data = [];
  40. foreach ($lists as $row) {
  41. $temp = [];
  42. foreach ($excelFields as $key => $excelField) {
  43. $fieldData = $row[$key];
  44. if (is_numeric($fieldData) && strlen($fieldData) >= 12) {
  45. $fieldData .= "\t";
  46. }
  47. $temp[$key] = $fieldData;
  48. }
  49. $data[] = $temp;
  50. }
  51. $spreadsheet = new Spreadsheet();
  52. $sheet = $spreadsheet->getActiveSheet();
  53. //设置单元格内容
  54. foreach ($title as $key => $value) {
  55. // 单元格内容写入
  56. $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
  57. }
  58. $row = 2; //从第二行开始
  59. foreach ($data as $item) {
  60. $column = 1;
  61. foreach ($item as $value) {
  62. //单元格内容写入
  63. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  64. $column++;
  65. }
  66. $row++;
  67. }
  68. $getHighestRowAndColumn = $sheet->getHighestRowAndColumn();
  69. $HighestRow = $getHighestRowAndColumn['row'];
  70. $column = $getHighestRowAndColumn['column'];
  71. $titleScope = 'A1:' . $column . '1';//第一(标题)范围(例:A1:D1)
  72. $sheet->getStyle($titleScope)
  73. ->getFill()
  74. ->setFillType(Fill::FILL_SOLID) // 设置填充样式
  75. ->getStartColor()
  76. ->setARGB('00B0F0');
  77. // 设置文字颜色为白色
  78. $sheet->getStyle($titleScope)->getFont()->getColor()
  79. ->setARGB('FFFFFF');
  80. // $sheet->getStyle('B2')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_YYYYMMDD);
  81. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  82. $allCope = 'A1:' . $column . $HighestRow;//整个表格范围(例:A1:D5)
  83. $sheet->getStyle($allCope)->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
  84. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  85. //创建excel文件
  86. $exportCache = new ExportCache();
  87. $src = $exportCache->getSrc();
  88. if (!file_exists($src)) {
  89. mkdir($src, 0775, true);
  90. }
  91. $writer->save($src . $this->fileName);
  92. //设置本地excel缓存并返回下载地址
  93. $vars = ['file' => $exportCache->setFile($this->fileName)];
  94. return (string)(url('adminapi/download/export', $vars, true, true));
  95. }
  96. /**
  97. * @notes 获取导出信息
  98. * @return array
  99. * @author 令狐冲
  100. * @date 2021/7/29 16:08
  101. */
  102. public function excelInfo()
  103. {
  104. $count = $this->count();
  105. $sum_page = max(ceil($count / $this->pageSize), 1);
  106. return [
  107. 'count' => $count, //所有数据记录数
  108. 'page_size' => $this->pageSize,//每页记录数
  109. 'sum_page' => $sum_page,//一共多少页
  110. 'max_page' => floor($this->pageSizeMax / $this->pageSize),//最多导出多少页
  111. 'all_max_size' => $this->pageSizeMax,//最多导出记录数
  112. 'page_start' => $this->pageStart,//导出范围页码开始值
  113. 'page_end' => min($sum_page, $this->pageEnd),//导出范围页码结束值
  114. 'file_name' => $this->fileName,//默认文件名
  115. ];
  116. }
  117. }