1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Alignment; public function export($data) { $column_params = [ [ 'title' => '订单号', 'index' => 'A', 'width' => '20', 'key' => 'order_no' ], [ 'title' => '订单总额', 'index' => 'B', 'width' => '10', 'key' => 'total' ], [ 'title' => '订单实付', 'index' => 'C', 'width' => '10', 'key' => 'receipts' ], [ 'title' => '优惠金额', 'index' => 'D', 'width' => '10', 'key' => 'coupon_amount' ], [ 'title' => '收货人', 'index' => 'E', 'width' => '10', 'key' => 'consignee' ], [ 'title' => '联系电话', 'index' => 'F', 'width' => '15', 'key' => 'mobile' ], [ 'title' => '收货地址', 'index' => 'G', 'width' => '45', 'key' => 'fullAddress' ], [ 'title' => '订单状态', 'index' => 'H', 'width' => '10', 'key' => 'status' ], [ 'title' => '下单时间', 'index' => 'I', 'width' => '20', 'key' => 'create_time' ], [ 'title' => '商品信息', 'index' => 'J', 'width' => '20', 'key' => 'goods' ], ]; $exclude = [ 'key' => 'goods', 'items' => [ [ 'title' => '商品标题', 'index' => 'J', 'width' => '20', 'key' => 'title' ], [ 'title' => '商品价格', 'index' => 'K', 'width' => '10', 'key' => 'sell_price' ], [ 'title' => '商品数量', 'index' => 'L', 'width' => '10', 'key' => 'qty' ], [ 'title' => '商品规格', 'index' => 'M', 'width' => '15', 'key' => 'spec' ], ] ]; $rowStart = 1; $rowIndex = 2; $spreadsheet = new Spreadsheet(); $activeSheet = $spreadsheet->getActiveSheet(); foreach ($column_params as $param) { if ($param['key'] != $exclude['key']) { $activeSheet->getColumnDimension($param['index'])->setWidth($param['width']); $activeSheet->setCellValue($param['index'] . $rowStart, $param['title']); } else { foreach ($exclude['items'] as $item) { $activeSheet->getColumnDimension($item['index'])->setWidth($item['width']); $activeSheet->setCellValue($item['index'] . $rowStart, $item['title']); } } } foreach ($data as $value) { $rowMergeEnd = false; foreach ($column_params as $param) { $rowChrIndex = $param['index'] . $rowIndex; if ($param['key'] != $exclude['key']) { $activeSheet->setCellValue($rowChrIndex, $value[$param['key']]); } else { foreach ($value[$param['key']] as $childIndex => $items) { foreach ($exclude['items'] as $item) { $rowChrChildIndex = $item['index'] . ($rowIndex + $childIndex); $activeSheet->setCellValue($rowChrChildIndex, $items[$item['key']]); } } } if (isset($value->{$exclude['key']})) { $merge = ($rowNumber = count($value->{$exclude['key']})) > 1; // 判断是否合并 } else { $merge = false; } if ($merge) { // 合并 $rowMergeEnd = $rowIndex + $rowNumber - 1; // 当前行 + 合并行数 $rowChrMergeEnd = $param['index'] . $rowMergeEnd; if ($param['key'] != $exclude['key']) { $activeSheet->mergeCells($rowChrIndex . ':' . $rowChrMergeEnd); } } } if ($rowMergeEnd) { $rowIndex = $rowMergeEnd; } $rowIndex = $rowIndex + 1; } $activeSheet->getStyle('N1')->applyFromArray($this->setExcelStyle()); $activeSheet->duplicateStyle( $spreadsheet->getActiveSheet()->getStyle('N1'), 'A1:M' . $rowIndex ); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header( "Content-Disposition:inline;filename=" . iconv( "utf-8", "GB2312//TRANSLIT", date('Y-m-d') . '.xlsx' ) ); header('Cache-Control: max-age=0');//禁止缓存 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } private function setExcelStyle() { $style = [ // //设置字体样式 // 'font' => [ // 'name' => 'Arial', // 'bold' => true, // 'italic' => false, // 'underline' => Font::UNDERLINE_DOUBLE, // 'strikethrough' => false, // 'color' => [ // 'rgb' => '808080' // ] // ], // //设置边框线样式 // 'borders' => [ // //allBorders所有的边框线样式 // //左边框线 // 'bottom' => [ // 'borderStyle' => Border::BORDER_DASHDOT, // 'color' => [ // 'rgb' => '808080' // ] // ], // //上边框线 // 'top' => [ // 'borderStyle' => Border::BORDER_DASHDOT, // 'color' => [ // 'rgb' => '808080' // ] // ] // ], //对齐样式 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, 'wrapText' => true, ], //是否使用前缀 'quotePrefix' => true ]; return $style; } |