控制层
//卖场销售明细,导出Excel
public function store_detail()
{
$data = input("get.");
$list = $this->storeOrderModel->getStoreModel($data);
$firstRowTitle = '卖场销售明细(';
$start_str = empty($data['start']) ? ' - ' : $data['start'];
$end_str = empty($data['end']) ? ' - ' : $data['end'];
$firstRowTitle .= '时间:' . $start_str . '至' . $end_str;
$firstRowTitle .= ')';
down_excel($list, '卖场销售明细表', $firstRowTitle, ['门店编号', '门店名称', '销售总额'], [20, 28, 20]);
}
模型层
/**
* 卖场销售明细
* @param $data
*/
public function getStoreModel($data)
{
//步骤1:从store_order表,查询订单价格,购买日期,导购员id,list
//步骤2:遍历list,根据userid,下单时间data,从表store_countoff查找门店id。
//步骤3:根据门店id,去store表,找到需要的门店名称,和store_gene_id,并赋值给新数组array
// 步骤4: 遍历array数组将金额累加得到门店的总金额;
$where = [];
foreach ($data as $k => $v) {
if ($v === 0) {
continue;
}
if (empty($v)) {
unset($data[$k]);
}
}
if (!empty($data['start'])&&!empty($data['end'])){
$start = strtotime($data['start']);
$end = strtotime($data['end']);
$where['create_time'] = ['between', [$start, $end]];
}
if(!empty($data['start'])&&empty($data['end'])){
$start = strtotime($data['start']);
unset($data['start']);
$where['create_time'] = ['>=', $start];
}
if(!empty($data['end'])&&empty($data['start'])){
$end = strtotime($data['end']);
unset($data['end']);
$where['create_time'] = ['<=', $end];
}
if (isset($data['region'])) {
$where['region']=array('=',$data['region']);
}
$kw = [];
if (!empty($data['kw'])){
unset($data['kw']);
}
// return $where;
//查询订单价格,购买日期,导购员id
$list = db('store_order')
->where($where)
->field('sales_amount,purchase_date,userid')->select();
$array = [];
foreach ($list as $k=>$v){
//根据导购员ID,下单时间在表store_countoff,查店铺ID
$store_id = db('store_countoff')
->where('userid',$v['userid'])
->where('date',$v['purchase_date'])
->distinct('store_id')
->value('store_id');
if (!empty($store_id)){
//得到ID之后再去store表查店铺信息
$store_name = db('store')
->where('store_id',$store_id)
->field('store_gene_id,store_name')->select();
$store = $store_name[0];
//讲订单的金额赋值给数组array
$store['count'] = $v['sales_amount'];
$array[] = $store;
}
}
$store_id = []; //得到所有store_id
$data = []; //新数组
foreach ($array as $key => $value) {
//不在数组中
if (!in_array($value['store_gene_id'],$store_id)) {
//store_id不存在于上面数组中,则添加该store_id到上面数组
$store_id[] = $value['store_gene_id'];
$data[] = $value;
} else { //在数组中
//循环$data数组
foreach ($data as $index => $item) {
//将相同store_id的count累加起来
if ($item['store_gene_id'] == $value['store_gene_id']) {
$data[$index]['count'] += $value['count'];
}
}
}
}
return $data;
}
公共类
/**
* @param $data 需要导出的数据 array
* @param $titleName 工作簿的名称 string
* @param $firstRowTitle 第一行头部标题 string
* @param $title 设置单元格标题 array
* @param $col 设置单元格宽度 array
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
function down_excel($data, $titleName, $firstRowTitle, $title, $col = [])
{
//1.创建新工作簿
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//默认的长度(共38个)
$letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL'];
//获取excel->title长度
$titleLen = count($data[0]);
$letter_count = count($letter); //38
for ($i = $titleLen; $i <= $letter_count; $i++) {
unset($letter[$i]);
}
//最大行数对应的$letter
$maxCell = $letter[$titleLen - 1];
//二维数组中的一维数组的key
$array_key = [];
foreach ($data as $key => $value) {
foreach ($value as $k => $v) {
$array_key[] = $k;
}
}
//设置默认行高
$sheet->getDefaultRowDimension()->setRowHeight(22);
//设置工作表标题
$sheet->setTitle($titleName);
//设置第一行头部样式
$titleStyleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
'font' => [
'name' => '宋体',
'size' => 14,
'bold' => true
]
];
//设置多少列 合并第一行A-V
$sheet->mergeCells('A1:' . $maxCell . 1)->getStyle('A1:' . $maxCell . 1)->applyFromArray($titleStyleArray);
$sheet->setCellValue('A1', $firstRowTitle)->getRowDimension('1')->setRowHeight(35);
//设置单元格标题
foreach ($letter as $key => $item) {
$sheet->setCellValue($item . '2', $title[$key]);
//垂直居中
$sheet->getPageSetup()->setHorizontalCentered(true);
}
/*--------------开始从数据库提取信息插入Excel表中 start------------------*/
$dataCount = count($data); //计算有多少条数据
//从第三行开始插入data数据
for ($i = 3; $i <= $dataCount + 2; $i++) {
$j = $i - 3; //数据下标从0开始
for ($z = 0; $z < $titleLen; $z++) {
//设置excel数据
$sheet->setCellValue('' . $letter[$z] . $i, $data[$j][$array_key[$z]]);
}
}
/*--------------开始从数据库提取信息插入Excel表中 end------------------*/
//设置标题单元格格式
$sheet->getStyle('A2:' . $maxCell . '2')->getFont()->setBold(true)->setName('宋体')->setSize(12);//Arial
//设置列宽
if ($col == []) {
for ($i = 0; $i < $letter_count; $i++) {
$col[$i] = 15;
}
}
foreach ($letter as $key => $item) {
$sheet->getColumnDimension($item)->setWidth($col[$key]);
}
//全部单元格设置对齐:水平居中对齐
$styleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
'font' => [
'size' => 12,
]
];
//设置所有单元格样式
$sheet->getStyle('A1:' . $maxCell . (count($data) + 2))->applyFromArray($styleArray);
//整个工作表设置为:自动筛选器区域
$sheet->setAutoFilter($spreadsheet->getActiveSheet()->calculateWorksheetDimension());
// 下载文件
$filename = $titleName . '-' . date('YmdHis', time()) . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
//3.从内存中清除工作簿
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}