// 次代码是在thinkPhp 框架中,导入的前提必须加载了phpExcel类 conds = $this->handleParams(I('get.')); // 总数 $total = $this->orderServ->count_by_conds($this->conds); // 生成缓存路径 $this->path = get_sitedir() .'enterprisemall'. D_S; rmkdir($this->path, 0777); if ($total < $this->zipLimit) { // excel $downloadPath = $this->createExec($this->fileName. rgmdate(NOW_TIME * 1000, 'Y-m-d')); // $excel = new Excel(); // $excel->make_excel_download($fileName, $this->colTitle, $this->width, []); } else { // 压缩包 $downloadPath = $this->zipExport($total); } // 下载 $this->downloadHeader($downloadPath); // 清理 $this->clear(); return true; } /** * 生成压缩包 * @param $total * @return string */ protected function zipExport($total) { set_time_limit(300); // 初始化 $zipServ = new \ZipArchive(); // 压缩名称 $zipName = $this->path. $this->fileName. rgmdate(NOW_TIME * 1000, 'Y-m-d'). '.zip'; // 生成压缩包 if (!file_exists($zipName)) { // 打开压缩包 $zipServ->open($zipName, \ZipArchive::CREATE); // 循环查询 for ($i = 0; $i < ceil($total / $this->zipLimit); $i++) { // 生成excel $fullFilename = $this->createExec($i, [$i * $this->zipLimit, $this->zipLimit]); // excel加入压缩包 if (!empty($fullFilename)) { $zipServ->addFile($fullFilename, $i. '.xls'); } } // 关闭句柄 $zipServ->close(); } return $zipName; } /** * 整合数据,生成excel * @param $excelName * @param array $pageOption * @return string * @throws \PHPExcel_Reader_Exception */ protected function createExec($excelName, $pageOption = []) { // 数据 $list = $this->listConds($pageOption); // 经销商信息 $dealerData = array_column($this->listDealer(['dealer_id' => array_column($list, 'dealer_id')], [], [], 'dealer_id, dealer_name, dealer_code'), NULL, 'dealer_id'); // 商品池子 $categoryData = $this->listCategory([], [], [], 'cate_id, parent_id, name'); // 订单商品信息 $goodData = $this->orderGoodData($list); $excelServ = new Excel(); // 表头 $i = 1; $a = 'A'; foreach ($this->colTitle as $keyTitle => $valTitle) { $excelServ->getActiveSheet()->setCellValue($a .$i, $valTitle); $excelServ->getActiveSheet() ->getStyle($a . $i) ->getFill() ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('009966'); $excelServ->getActiveSheet() ->getColumnDimension($a) ->setWidth($this->width[$keyTitle] . "pt"); $a++; } // 数据 foreach ($list as $key => $val) { $excelServ->getActiveSheet()->setCellValue('A' .++$i, empty($val['order_sn']) ? '' : $val['order_sn']); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('B' .$i, rgmdate($val['created'], 'Y-m-d H:i')); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('C' .$i, empty($val['po_numbers']) ? '' : $val['po_numbers']); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('D' .$i, empty($dealerName = $dealerData[$val['dealer_id']]['dealer_name']) ? '' : $dealerName); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('E' .$i, empty($dealerCode = $dealerData[$val['dealer_id']]['dealer_code']) ? '' : $dealerCode); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('F' .$i, ''); $excelServ->getActiveSheet()->setCellValue('G' .$i, '', 'n'); $excelServ->getActiveSheet()->setCellValue('H' .$i, ''); $excelServ->getActiveSheet()->setCellValue('I' .$i, (float)$val['money'], 'n'); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('J' .$i, (float)$val['freight'], 'n'); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('K' .$i, empty($cateName = $this->getCategory($categoryData, $val['cat_id'])) ? '' : $cateName); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('L' .$i, $val['cancel_status'] == self::CANCEL_VALUE ? self::CANCEL_CHINESE_VALUE : $this->orderStatus[$val['order_status']]); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $goods = empty($goodData[$val['order_sn']]) ? [] : $goodData[$val['order_sn']]; foreach ($goods as $valGood) { $excelServ->getActiveSheet()->setCellValue('F' .++$i, empty($valGood['goods_name']) ? '' : $valGood['goods_name']); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('G' .$i, (int)$valGood['goods_quantity'], 'n'); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('H' .$i, empty($valGood['supplier_name']) ? '' : $valGood['supplier_name']); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $excelServ->getActiveSheet()->setCellValue('I' .$i, (float)$valGood['goods_money'], 'n'); $excelServ->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); } $dealerName = $dealerCode = ''; } // excel路径 $excelPath = $this->path. $excelName. '.xls'; // 保存 $objWriter = \PHPExcel_IOFactory::createWriter($excelServ, 'Excel5'); $objWriter->save($excelPath); return $excelPath; } /** * 数据列表 * @param $pageOption * @return array|bool */ protected function listConds($pageOption) { $filed = 'order_id, order_sn, created, po_numbers, dealer_id, money, cat_id, order_status, freight, cancel_status'; $list = $this->orderServ->list_by_conds($this->conds, $pageOption, ['created' => 'DESC'], $filed); return $list; } /** * 下载 * @param $downloadPath * @return bool */ protected function downloadHeader($downloadPath) { if (!file_exists($downloadPath)) { exit('下载失败'); } $file = fopen($downloadPath, "r"); Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length: " . filesize($downloadPath)); Header("Content-Disposition: attachment; filename=". basename($downloadPath)); echo fread($file, filesize($downloadPath)); $buffer = 1024; while (!feof($file)) { $file_data = fread($file, $buffer); echo $file_data; } fclose($file); return true; } /** * 清理产生的临时文件 * @return bool */ protected function clear() { $dh = opendir($this->path); while ($file = readdir($dh)) { if ($file != "." && $file != "..") { unlink($this->path . $file); } } return true; } }