博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
压缩包导出excel
阅读量:5749 次
发布时间:2019-06-18

本文共 8417 字,大约阅读时间需要 28 分钟。

// 次代码是在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; } }

转载于:https://www.cnblogs.com/taikongliu/p/8514784.html

你可能感兴趣的文章
linux文件权限与属性的更改
查看>>
Jquery绑定事件
查看>>
android 资源种类及使用
查看>>
Explorer程序出错
查看>>
java之抽象工厂
查看>>
log4j2性能剖析
查看>>
修改系统时间 ubuntu
查看>>
Centos7同时运行多个Tomcat
查看>>
使用CocoaPods过程中的几个问题
查看>>
我的友情链接
查看>>
mysql数据类型---数值型---int
查看>>
为eclipse安装maven插件
查看>>
Android自动化压力测试——Monkey工具
查看>>
公司新年第一次全员大会小记
查看>>
最懒的程序员
查看>>
了解Amdahl定理,该定理再多核时代有怎样的影响?
查看>>
JAVA8 Stream 浅析
查看>>
inner join on, left join on, right join on要详细点的介绍
查看>>
SAS vs SSD对比测试MySQL tpch性能
查看>>
Spring boot 整合CXF webservice 全部被拦截的问题
查看>>