一、在系统Extend下面的Vendor新建文件夹excel,然后把PHPExcel.php和PHPExcel文件夹放入之前创建的excel中
二、创建exportAction
/**
*构造函数
*
*@author Julian
*/
protected function _initialize() {
Vendor('Excel/PHPExcel');
Vendor('Excel/PHPExcel/Writer/Excel5');//导入vendor里面的第三方excel扩展
}
/**
*用支付宝付款填写表单
*
*@author Julian
*/
public function index(){
$pay_orders = $_GET['pay_orders'];
//$pay_orders = substr($pay_orders,0,strlen($pay_orders)-1);
$orders = M('orders')->where(" id in ($pay_orders)")->select();
//实例化PHPExcel类
$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
//保存为文件
$outputFileName = 'xsd_'. time() . '.xls' ;
// echo $outputFileName;
//var_dump($objPHPExcel);
// die();
$objProps = $objPHPExcel->getProperties();
//echo $objProps;
// var_dump($objPHPExcel);
// die();
//创建人
$objProps->setCreator("Julian Jiang");
//设置当前的sheet
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet ();
//var_dump($objActSheet);
//设置sheet的name
$objActSheet->setTitle('template.xls');
$objActSheet->getColumnDimension('A')->setWidth(30);
$objActSheet->getColumnDimension('B')->setWidth(30);
$objActSheet->getColumnDimension('C')->setWidth(30);
$objActSheet->getColumnDimension('D')->setWidth(30);
$objActSheet->getColumnDimension('E')->setWidth(30);
$objActSheet->getColumnDimension('F')->setWidth(30);
//设置单元格的值,设置总计的选项的名称
$objActSheet->setCellValue ( 'A1', '批次号');
$objActSheet->setCellValue ( 'B1', '付款日期');
$objActSheet->setCellValue ( 'C1', '付款人email');
$objActSheet->setCellValue ( 'D1', '账户名称');
$objActSheet->setCellValue ( 'E1', '总金额(元)');
$objActSheet->setCellValue ( 'F1', '总笔数');
//设置子单元格的值,设置子单元格的选项的名称
$objActSheet->setCellValue ( 'A3', '商户流水号');
$objActSheet->setCellValue ( 'B3', '收款人email');
$objActSheet->setCellValue ( 'C3', '收款人姓名');
$objActSheet->setCellValue ( 'D3', '付款金额(元)');
$objActSheet->setCellValue ( 'E3', '付款理由');
$all_money = 0;
for ($i=0; $i < count($orders); $i++) {
# code...
$Model = new Model();
//进行原生的SQL查询
$alipay_info = $Model->query('select ai9me_pay_set.aseller_email,ai9me_pay_set.pay_name from ai9me_wxuser
left join ai9me_pay_set on ai9me_wxuser.token = ai9me_pay_set.token where wxid = '.$orders[$i]['seller_id']);
$objActSheet->setCellValue ( 'A'.($i+4), $i+1);
$objActSheet->setCellValue ( 'B'.($i+4), $alipay_info[0]['aseller_email']);
$objActSheet->getCell('B'.($i+4))->getHyperlink()->setUrl('ailto:'.$alipay_info[0]['aseller_email']);
$objActSheet->setCellValue ( 'C'.($i+4), $alipay_info[0]['pay_name']);
$objActSheet->setCellValue ( 'D'.($i+4), $orders[$i]['product_num'] * $orders[$i]['product_price']);
$objActSheet->setCellValue ( 'E'.($i+4), '购买商品');
$all_money += $orders[$i]['product_num'] * $orders[$i]['product_price'] ;
}
$objActSheet->setCellValue('A2', date('YmdHis').rand(1000,9999));
$objActSheet->setCellValue('B2',date('Y-m-d H:i:s'));
$objActSheet->setCellValue('C2', '3099386@qq.com');
$objActSheet->getCell('C2')->getHyperlink()->setUrl('mailto:3099386@qq.com');
$objActSheet->setCellValue('D2', '杭州汇之界网络科技有限公司');
$objActSheet->setCellValue('E2', $all_money);
$objActSheet->setCellValue('F2', count($orders));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outputFileName.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
redirect('http:www.baidu.com');
}