直接上代码:
//导出excel
public function excel()
{
//引入相关文件
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Worksheet.Drawing");
import("Org.Util.PHPExcel.Writer.Excel2007");
$model = M('user');
//用户信息
$data = $model
->field('id,alias,login_time,create_time')
->select();
$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
//新建第一个工作表
$objActSheet = $objPHPExcel->getActiveSheet();
// 水平居中(位置很重要,建议在最初始位置)
// $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->setCellValue('A1', 'alias');
$objActSheet->setCellValue('B1', '登录时间');
$objActSheet->setCellValue('C1', '注册时间');
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
// 垂直居中
// $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//向表格中插入数据
foreach ($data as $k => $v) {
$k += 2;
$objActSheet->setCellValue('A' . $k, $v['alias']);
$login_time = $v['login_time'] > 0 ? date('Y-m-d H:i:s', $v['login_time']) : '';
$objActSheet->setCellValue('C' . $k, $login_time);
$create_time = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
$objActSheet->setCellValue('B' . $k, $create_time);
}
//重命名表,这个名称是表格下方的名称
$objPHPExcel->getActiveSheet()->setTitle('用户行为');
//新建第二个工作表,第二个表中的数据是对第一个表中行为的统计
$objActSheet1 = $objPHPExcel->createSheet();
//这个工作表的列名我们用数据库'gift'表中的数据
$gift_list = M('gift')->field('id,listorder,title')->order('listorder asc,id desc')->select();
// Excel表格的列名数组,用于写入数据所用
$cell_name = [0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z', 26 => 'AA', 27 => 'AB', 28 => 'AC', 29 => 'AD', 30 => 'AE', 31 => 'AF', 32 => 'AG', 33 => 'AH', 34 => 'AI', 35 => 'AJ', 36 => 'AK', 37 => 'AL', 38 => 'AM', 39 => 'AN'];
//第一列列名是‘alias’
$objActSheet1->setCellValue('A1', 'alias');
//循环‘user’表中的数据
foreach ($data as $k => $v) {
//A列中的值都是‘user’表中的 alias 值
$objActSheet1->setCellValue('A' . ($k + 2), $v['alias']);
//循环‘gift'中的数据,生成列名,并加入数据
foreach ($gift_list as $key => $item) {
//列名,从第二列开始
$objActSheet1->setCellValue($cell_name[$key + 1] . '1', $item['title']);
// 设置表格宽度
//$objActSheet1->getColumnDimension($cell_name[$key])->setWidth(20);
//计算数量
$count = M('gift_log')->where(['user_id' => $v['id'], 'gift_id' => $item['id']])->count();
//插入数据
$objActSheet1->setCellValue($cell_name[$key + 1] . ($k + 2), $count);
}
}
//重命名表,这个名称是表格下方的名称
$objActSheet1->setTitle('中奖记录');
//新建第三个表 。。。
//$objActSheet2 = $objPHPExcel->createSheet();
//do something
//设置活动单指数到第一个表,所以Excel打开时,是第一个表
$objPHPExcel->setActiveSheetIndex(0);
$fileName = '用户表_';
$date = date("Y-m-d", time());
$fileName .= "{$date}.xls";
$fileName = iconv("utf-8", "gb2312", $fileName);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename="$fileName"");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); //文件通过浏览器下载
}
相关文章:数据导入