ListsExcelTrait.php 5.3 KB

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