新版订单消耗系统

XLSXWriteClass.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2023/5/6
  6. * Time: 18:23
  7. */
  8. namespace App\libs\XLSXWriter;
  9. class XLSXWriteClass
  10. {
  11. private $write;
  12. public function __construct()
  13. {
  14. $this->write = new XLSXWriter(); //如果提示不存在,就引入一下
  15. }
  16. /**
  17. * 把数据下载为文件
  18. * @param array $title 标题数据
  19. * @param array $data 内容数据
  20. * @param string $fileName 文件名(可包含路径)
  21. * @param int $type Excel下载类型:1-下载文件;2-浏览器弹框下载
  22. * @param string $sheet Excel的工作表
  23. */
  24. public function downloadExcel($title, $data, $fileName, $type = 1, $sheet = 'Sheet1')
  25. {
  26. if ($type == 2) {
  27. //设置 header,用于浏览器下载
  28. header('Content-disposition: attachment; filename="' . XLSXWriter::sanitize_filename($fileName) . '"');
  29. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  30. header('Content-Transfer-Encoding: binary');
  31. header('Cache-Control: must-revalidate');
  32. header('Pragma: public');
  33. }
  34. //处理标题数据,都设置为string类型
  35. $header = [];
  36. foreach ($title as $value) {
  37. $header[$value] = 'string'; //把表头的数据全部设置为string类型
  38. }
  39. $this->write->writeSheetHeader($sheet, $header);
  40. //根据标题数据title,按title的字段顺序把数据一条条加到excel中
  41. foreach ($data as $key => $value) {
  42. $row = [];
  43. foreach ($title as $k => $val) {
  44. $row[] = $value[$k];
  45. }
  46. $this->write->writeSheetRow($sheet, $row);
  47. }
  48. if ($type == 1) { //直接保存文件
  49. $this->write->writeToFile($fileName);
  50. } else if ($type == 2) { //浏览器下载文件
  51. $this->write->writeToStdOut();
  52. // echo $this->write->writeToString();
  53. exit(0);
  54. } else {
  55. die('文件下载方式错误~');
  56. }
  57. }
  58. /**
  59. * 设置excel第一行的数据
  60. * @param array $title 标题数据,格式如:['A1' => '姓名', 'B1' => '爱好']
  61. * @param string $sheet excel工作区,默认第一个
  62. * @return void
  63. */
  64. public function setTitle($title, $sheet = 'Sheet1')
  65. {
  66. //处理标题数据,都设置为string类型
  67. $header = [];
  68. foreach ($title as $value) {
  69. $header[$value] = 'string'; //把表头的数据全部设置为string类型
  70. }
  71. $this->write->writeSheetHeader($sheet, $header);
  72. }
  73. /**
  74. * 根据标题顺序,设置每一行的数据
  75. * @param array $data 一行数据
  76. * @param array $title 标题数据
  77. * @param string $sheet excel工作区,默认第一个
  78. * @return void
  79. */
  80. public function setOneRowData($data, $title, $sheet = 'Sheet1')
  81. {
  82. $row = [];
  83. foreach ($title as $k => $val) {
  84. $row[] = $data[$k];
  85. }
  86. $this->write->writeSheetRow($sheet, $row);
  87. }
  88. /**
  89. * 设置文件保存的位置
  90. * @param string $fileName 保存的文件名(可包含路径)
  91. * @return void
  92. */
  93. public function setSaveFile($fileName)
  94. {
  95. $this->write->writeToFile($fileName);
  96. }
  97. }