今天做的时候发现有好多答案,很乱,所以就整理了一个,以便下回使用

js代码

$("#excel").click(function(){
var _date=$("#date").val();
var _id=$("#date").attr('group_id');
window.location.href="groupbuying.php?act=group_info&date="+_date+"&id="+_id+"&type=excel";
php代码
include './phpexcel1/Classes/PHPExcel.php';
$excel = new PHPExcel();
//列数
$letter = array('A','B','C','D','E','F','G','H','I','J','K');
//表头
$tableheader = array('商品名称','国条码','商品口味','进货数量','进货价','销售价','销售额','下单时间','区域','买家名称','供应商名称');
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
//给表赋值 这里的info 换成相应的数组
for ($i = 2;$i <= count($info) + 1;$i++) {
$j = 0;
foreach ($info[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
$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="'.$group_id.'.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');exit;

posted on 2017-08-15 15:18  这世界太多来去无常  阅读(261)  评论(0编辑  收藏  举报