码迷,mamicode.com
首页 > 其他好文 > 详细

压缩包导出excel

时间:2018-03-06 17:01:55      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:pex   des   add   option   cancel   ant   arch   mon   setfill   

// 次代码是在thinkPhp 框架中,导入的前提必须加载了phpExcel类
<?php
/**
* ProjectName: oa2
* Description: 订单管理 - 导出
* FileName : ExportController.class.php
* User : 刘瑞
* Date : 17-8-31
* Time : 下午3:08
**/

namespace Apicp\Controller\Order;

use Com\Excel;

class ExportController extends AbstractController
{
/**
* 是否必须登录
*
* @var string $_require_login
*/
protected $_require_login = false;

/**
* 设置导出列宽
* @var array
*/
protected $width = [25, 18, 15, 25, 18, 30, 10, 20, 15, 10, 25, 15];

/**
* 设置导出表头
* @var array
*/
protected $colTitle = [‘订单编号‘, ‘订单时间‘, ‘PO单号‘, ‘经销商名称‘, ‘经销商编码‘, ‘商品名称‘, ‘商品数量‘, ‘供应商‘, ‘消耗总积分‘, ‘运费‘, ‘商品POOL‘, ‘订单状态‘];

/**
* 文件名
* @var string
*/
protected $fileName = ‘订单列表‘;

/**
* 订单状态
* @var string
*/
protected $orderStatus = [‘‘, ‘待付款‘, ‘待发货‘, ‘待收货‘, ‘已签收‘, ‘待确认‘];

/**
* excel最大数据数
* @var int
*/
protected $zipLimit = 50000;

/**
* 生成压缩包EXCEL文件夹
* @var string
*/
protected $path = ‘‘;

/**
* 条件
* @var array
*/
protected $conds = [];

/**
* 已取消中文
*/
const CANCEL_CHINESE_VALUE = ‘已取消‘;

/**
* 入口方法
* @return bool
*/
public function Index()
{
// 条件
$this->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;
}

}

压缩包导出excel

标签:pex   des   add   option   cancel   ant   arch   mon   setfill   

原文地址:https://www.cnblogs.com/taikongliu/p/8514784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!