OpenSpoutWriter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace plugin\saiadmin\service;
  3. use OpenSpout\Common\Entity\Style\Border;
  4. use OpenSpout\Common\Entity\Style\BorderPart;
  5. use OpenSpout\Writer\XLSX\Writer;
  6. use OpenSpout\Common\Entity\Row;
  7. use OpenSpout\Common\Entity\Style\Style;
  8. /**
  9. * Excel写入类
  10. * OpenSpout
  11. */
  12. class OpenSpoutWriter
  13. {
  14. /**
  15. * 操作实例
  16. * @var Writer
  17. */
  18. protected $instance;
  19. /**
  20. * 文件路径
  21. * @var string
  22. */
  23. protected $filepath;
  24. /**
  25. * 初始化
  26. * @param string $fileName 文件名称
  27. */
  28. public function __construct(string $fileName)
  29. {
  30. $this->filepath = $this->getFileName($fileName);
  31. $this->instance = new Writer();
  32. $this->instance->openToFile($this->filepath);
  33. }
  34. /**
  35. * 获取完整的文件路径
  36. * @param string $fileName
  37. * @return string
  38. */
  39. public function getFileName(string $fileName): string
  40. {
  41. $path = config('plugin.saiadmin.saithink.export_path',base_path() . '/plugin/saiadmin/public/export/');
  42. @mkdir($path, 0777, true);
  43. return $path . $fileName;
  44. }
  45. /**
  46. * 设置表格宽度
  47. * @param array $width 宽度数组
  48. * @return void
  49. */
  50. public function setWidth(array $width = [])
  51. {
  52. if (empty($width)) {
  53. return;
  54. }
  55. $sheet = $this->instance->getCurrentSheet();
  56. foreach ($width as $key => $value) {
  57. $sheet->setColumnWidth($value, $key + 1);
  58. }
  59. }
  60. /**
  61. * 设置表头
  62. * @param array $header 表头数组
  63. * @param $style
  64. * @return void
  65. */
  66. public function setHeader(array $header = [], $style = null): void
  67. {
  68. if (empty($style)) {
  69. $border = new Border(
  70. new BorderPart("top", "black", "thin"),
  71. new BorderPart("right", "black", "thin"),
  72. new BorderPart("bottom", "black", "thin"),
  73. new BorderPart("left", "black", "thin"),
  74. );
  75. $style = new Style();
  76. $style->setFontBold();
  77. $style->setCellAlignment("center");
  78. $style->setBorder($border);
  79. }
  80. $rowFromValues = Row::fromValues($header, $style);
  81. $this->instance->addRow($rowFromValues);
  82. }
  83. /**
  84. * 设置数据
  85. * @param array $data 数据数组
  86. * @param $style
  87. * @return void
  88. */
  89. public function setData(array $data = [], $style = null, array $filter = []): void
  90. {
  91. if (empty($style)) {
  92. $border = new Border(
  93. new BorderPart("top", "black", "thin"),
  94. new BorderPart("right", "black", "thin"),
  95. new BorderPart("bottom", "black", "thin"),
  96. new BorderPart("left", "black", "thin"),
  97. );
  98. $style = new Style();
  99. $style->setCellAlignment("center");
  100. $style->setBorder($border);
  101. }
  102. foreach($data as $row) {
  103. if (!empty($filter)) {
  104. foreach ($filter as $key => $value) {
  105. foreach ($value as $item) {
  106. if ($item['value'] == $row[$key]) {
  107. $row[$key] = $item['label'];
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. $rowFromValues = Row::fromValues($row, $style);
  114. $this->instance->addRow($rowFromValues);
  115. }
  116. }
  117. /**
  118. * 获取文件
  119. * @return string
  120. */
  121. public function returnFile(): string
  122. {
  123. $this->instance->close();
  124. return $this->filepath;
  125. }
  126. }