tp导出excel多标签
优先安装扩展包 composer require phpoffice/phpspreadsheet 代码里引用 use \PhpOffice\PhpSpreadsheet\IOFactory; use \PhpOffice\PhpSpreadsheet\Spreadsheet;
public function export_all(){
$find = Db::name('user_cost_log')->whereTime('price_time','today')->find();
if(empty($find)){
$this->error('今日数据还没有生成请生成后导出');
}
$jc_class = Db::name('jc_class')
->field('jc.id,jc.sid,jc.gid,jc.classname,js.schoolname,jg.gradename')
->alias('jc')
->join('jc_school js','jc.sid = js.id')
->join('jc_grade jg','jc.gid = jg.id')
->select();
foreach($jc_class as $key=>$value){
$jc_class[$key]['cost'] = db::name('user_cost_log')
->field('uc.childname,u.truename,u.mobile,js.schoolname,jg.gradename,jc.classname')
->alias('ucl')
->join('user_child uc','uc.id = ucl.child_id')
->join('user u','u.id = uc.parent_id')
->join('jc_school js','uc.sid = js.id')
->join('jc_grade jg','uc.gid = jg.id')
->join('jc_class jc','uc.cid = jc.id')
->where('uc.cid',$value['id'])
->whereTime('ucl.price_time','today')
->select();
if(empty($jc_class[$key]['cost'])){
unset($jc_class[$key]);
}
}
$dataarr=[];
$row1[] = array(
'0'=>'学生姓名',
'1'=>'家长姓名',
'2'=>'联系方式',
'3'=>'所在学校',
'4'=>'所在年级',
'5'=>'所在班级',
);
foreach(array_values($jc_class) as $k=>$val){
$dataarr[$k]['title'] = $val['schoolname'].$val['gradename'].$val['classname'];
$dataarr[$k]['info'] = array(
"学校:" => $val['schoolname'],
"年级:" => $val['gradename'],
"班级:" => $val['classname'],
"配送方:" => '远行餐饮',
"配送日期:" => date('Y-m-d',time()),
"配餐量:" => count($val['cost']),
);
$key_name = [];
foreach($val['cost'] as $key=>$value){
$key_name[] = array_values($value);
}
$dataarr[$k]['rows'] = array_merge($row1,$key_name);
}
$this->xtexport($dataarr);
}
public function xtexport($data_array)
{
$time = date("Ymd", time());
$spreadsheet = new Spreadsheet();
foreach ($data_array as $key => $data) {
$this->opSheet($spreadsheet,$key,$data);
}
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=".$time.'订餐名单'.".xlsx");
header('Cache-Control: max-age=0');
$objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');
$objWriter->save('php://output');
exit;
}
public function opSheet($spreadsheet,$n, $data)
{
$spreadsheet->createSheet();
$objActSheet = $spreadsheet->setActiveSheetIndex($n);
$keys = $data['rows'][0];
$count = count($keys);
$infoNum = ceil(count($data['info']) / 2);
$infoStart = $infoNum + 2 ;
$cellName = array('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', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
$sheet = $spreadsheet->getActiveSheet($n)->setTitle($data['title']);
$spreadsheet->getActiveSheet($n)->mergeCells('A1:' . $cellName[$count - 1] . '1');
$spreadsheet->getActiveSheet($n)->getStyle('A1')->getFont()->setSize(20);
$spreadsheet->getActiveSheet($n)->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('A')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('B')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('C')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('D')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('E')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle('F')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle("$infoStart")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($n)->getStyle("$infoStart")->getFont()->setBold(true);
$objActSheet->setCellValue('A1', $data['title']);
foreach ($data['rows'] as $key => $item)
{
for ($i = 65; $i < $count + 65; $i++)
{
$sheet->setCellValue(strtoupper(chr($i)) . ($key+"$infoStart"), $item[$i - 65]);
$spreadsheet->getActiveSheet($n)->getColumnDimension(strtoupper(chr($i)))->setWidth(20);
}
}
$rowNumber = 1;
$infoIndex = 0;
foreach ($data['info'] as $key => $value) {
if ($infoIndex % 2 == 0) {
$rowNumber++;
$infoCellName1 = 'A' . $rowNumber;
$infoCellMegreRange = 'B' . $rowNumber . ':C' . $rowNumber;
$infoCellName2 = 'B' . $rowNumber;
} else {
$infoCellName1 = 'D' . $rowNumber;
$infoCellMegreRange = 'E' . $rowNumber . ':F' . $rowNumber;
$infoCellName2 = 'E' . $rowNumber;
}
$spreadsheet->setActiveSheetIndex($n)->setCellValue($infoCellName1, $key);
$spreadsheet->getActiveSheet($n)->mergeCells($infoCellMegreRange);
$spreadsheet->setActiveSheetIndex($n)->setCellValue($infoCellName2, $value);
$infoIndex++;
}
}
tp导出excel多标签.zip
|