PHPExcel导出
方法一、
https://blog.csdn.net/u014236259/article/details/60601767
public function ExportExcelOrder($data, $name)
{
vendor('PHPExcel.PHPExcel'); //引入PHPExcel类
$objPHPExcel = new \PHPExcel(); //实例化 相当于在桌面新建一个excel
// 表头
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'id')
->setCellValue('B1', '订单编号')
->setCellValue('C1', '收货人姓名')
->setCellValue('D1', '收货人电话')
->setCellValue('E1', '地址')
->setCellValue('F1', '省份')
->setCellValue('G1', '返现金额')
->setCellValue('H1', '品牌名称');
//设置内容水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(24);
//设置单元格自动宽度
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
// 内容
for ($i = 0, $len = count($data); $i < $len; $i++) {
$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($i + 2), $data[$i]['part_order_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 2), $data[$i]['order_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($i + 2), $data[$i]['accept_name']);
$objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 2), $data[$i]['phone']);
$objPHPExcel->getActiveSheet(0)->setCellValue('E' . ($i + 2), $data[$i]['address']);
$objPHPExcel->getActiveSheet(0)->setCellValue('F' . ($i + 2), $data[$i]['province_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('G' . ($i + 2), $data[$i]['cashback']);
$objPHPExcel->getActiveSheet(0)->setCellValue('H' . ($i + 2), $data[$i]['brand_name']);
//设置内容水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle($i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
$objPHPExcel->getActiveSheet()->setTitle('User');
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();//清楚缓存区,解决乱码问题
header("Pragma:public");
header("Content-Type:application/x-msexecl;name=\"{$name}.xlsx\"");
header("Content-Disposition:inline;filename=\"{$name}.xlsx\"");
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');
exit;
}
//导出excel
vendor('PHPExcel.PHPExcel'); //引入PHPExcel类
$objPHPExcel = new \PHPExcel(); //实例化 相当于在桌面新建一个excel
// 表头
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'id')
->setCellValue('B1', '订单编号')
->setCellValue('C1', '收货人姓名')
->setCellValue('D1', '收货人电话')
->setCellValue('E1', '地址')
->setCellValue('F1', '省份')
->setCellValue('G1', '返现金额')
->setCellValue('H1', '品牌名称');
//设置内容水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(24);
//设置单元格自动宽度
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
// 内容
for ($i = 0, $len = count($data); $i < $len; $i++) {
$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($i + 2), $data[$i]['part_order_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 2), $data[$i]['order_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($i + 2), $data[$i]['accept_name']);
$objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 2), $data[$i]['phone']);
$objPHPExcel->getActiveSheet(0)->setCellValue('E' . ($i + 2), $data[$i]['address']);
$objPHPExcel->getActiveSheet(0)->setCellValue('F' . ($i + 2), $data[$i]['province_id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('G' . ($i + 2), $data[$i]['cashback']);
$objPHPExcel->getActiveSheet(0)->setCellValue('H' . ($i + 2), $data[$i]['brand_name']);
//设置内容水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle($i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
$objPHPExcel->getActiveSheet()->setTitle('User');
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();//清楚缓存区,解决乱码问题
header("Pragma:public");
header("Content-Type:application/x-msexecl;name=\"{$name}.xlsx\"");
header("Content-Disposition:inline;filename=\"{$name}.xlsx\"");
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');
exit;
}
//导出excel
public function push()
{
$data = M()->table("part_order")->where(['user_id'=>1583])->select();
$fileName = "导出订单时间".date('Ymd');
$this->ExportExcelOrder($data, $fileName);
}
方法二、
public function export($title,$data,$fileName)
{
vendor('PHPExcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
//横向单元格标识
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
$objPHPExcel->getActiveSheet(0)->setTitle('我的订单'); //设置sheet名称
$_row = 1; //设置纵向单元格标识
if($title){
$_cnt = count($title);
//合并单元格 设置字体加粗 字号
$objPHPExcel->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row)->getStyle()
->getFont()->setBold(true)->setSize(13);
//字体水平居中
$objPHPExcel->getActiveSheet(0)->getStyle('A'.$_row.':'.$cellName[$_cnt-1].$_row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置合并后的单元格内容
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$_row, '订单导出:'.date('Y-m-d H:i:s'));
$_row++;
$i = 0;
foreach($title AS $v){
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cellName[$i].$_row)->setAutoSize(true);//设置单元格自动宽度
$objPHPExcel->setActiveSheetIndex(0)->getStyle($cellName[$i].$_row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);//单个单元格居左
//设置列标题
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);
$i++;
}
$_row++;
}
//填写数据
if($data){
$i = 0;
foreach($data AS $_v){
$j = 0;
foreach($_v AS $_cell){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);
$j++;
}
$i++;
}
}
//文件名处理
if(!$fileName){
$fileName = uniqid(time(),true);
}
header("Pragma:public");
header("Content-Type:application/x-msexecl;name=\"{$fileName}.xlsx\"");
header("Content-Disposition:inline;filename=\"{$fileName}.xlsx\"");
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');
exit;
}
//导出excel
public function pull()
{
$title = ['id','订单编号','收货人姓名','收货人电话','地址','省份','返现金额','品牌名称'];
$data = M()->table("part_order")
->field("part_order_id,order_id,accept_name,accept_phone,address,province_id,cashback,brand_name")
->where(['user_id'=>1583])->select();
$fileName = "导出订单时间".date('Ymd');
$this->export($title,$data,$fileName);
}