php 导出Excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 导出Excel相关的知识,希望对你有一定的参考价值。
先下载phpExcel,百度搜索进官网下载
导入require(dirname(__FILE__) . ‘/phpexcel/Classes/PHPExcel.php‘);
代码:
//创建对象
$excel = new PHPExcel();
//Excel表格式,8列
$letter = array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘);
//表头数组
$tableheader = array(‘订单号‘,‘下单时间‘,‘用户名‘,‘收货人‘,‘联系电话‘,‘详细地址‘,‘应付金额‘,‘付款状态‘,‘发货状态‘,‘订单商品‘);
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
//表格宽度
$excel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘H‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘I‘)->setWidth(30);
$excel->getActiveSheet()->getColumnDimension(‘J‘)->setWidth(50);
//查詢值
$start_time=strtotime($_REQUEST[‘start_time‘]);
$end_time=strtotime($_REQUEST[‘end_time‘]);
$pay_status=$_REQUEST[‘pay_status‘];
$shipping_status=$_REQUEST[‘shipping_status‘];
$sql = "SELECT o.order_id,o.order_sn,o.add_time,o.user_id,o.consignee,o.tel,o.address,o.order_amount,o.order_status,o.pay_status,o.shipping_status" .
" FROM " . $ecs->table(‘order_info‘) . " AS o ".
"WHERE o.add_time > ‘{$start_time}‘ AND o.add_time < ‘{$end_time}‘ AND pay_status={$pay_status} AND shipping_status={$shipping_status} ";
$res = $db->getAll($sql);
$user_list = $res;
//填充表格信息
$hang = 2;
foreach ($user_list as $key=>$value) {
$goods=$db->getAll("select goods_name,goods_number from " . $ecs->table(‘order_goods‘) . " where order_id=".$value[‘order_id‘]);
$goodsname=" ";
foreach($goods as $k1=>$v1){
$goodsname.="\n".$v1[‘goods_name‘]."x".$v1[‘goods_number‘]." ";
}
$username=$db->getOne("select user_name from " . $ecs->table(‘users‘) . " where user_id=".$value[‘user_id‘]);
$excel->getActiveSheet()->setCellValue(‘A‘ . ($hang), $value[‘order_sn‘]." ");//加个空格,防止时间戳被转换
$excel->getActiveSheet()->setCellValue(‘B‘ . ($hang), date(‘Y-m-d H:i:s‘, $value[‘add_time‘])." ");
$excel->getActiveSheet()->setCellValue(‘C‘ . ($hang), $username." ");
$excel->getActiveSheet()->setCellValue(‘D‘ . ($hang), $value[‘consignee‘]." ");
$excel->getActiveSheet()->setCellValue(‘E‘ . ($hang), $value[‘tel‘]." ");
$excel->getActiveSheet()->setCellValue(‘F‘ . ($hang), $value[‘address‘]." ");
$excel->getActiveSheet()->setCellValue(‘G‘ . ($hang), $value[‘order_amount‘]." ");
$excel->getActiveSheet()->setCellValue(‘H‘ . ($hang), $_LANG[‘ps‘][$value[‘pay_status‘]]." "); $excel->getActiveSheet()->setCellValue(‘I‘ . ($hang), $_LANG[‘ss‘][$value[‘shipping_status‘]]." ");
$excel->getActiveSheet()->setCellValue(‘J‘ . ($hang), $goodsname);
$excel->getActiveSheet()->getStyle(‘A2:T‘.$hang)->getAlignment()->setWrapText(true); //自动调节行高
$hang = $hang + 1;
}
//创建Excel输入对象
$write = new PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header(‘Content-Disposition:attachment;filename=订单列表列表‘.date(‘Y-m-d‘,time()).‘.xls‘);
header("Content-Transfer-Encoding:binary");
$write->save(‘php://output‘);
exit;
以上是关于php 导出Excel的主要内容,如果未能解决你的问题,请参考以下文章