项目中遇到了一个到 处,之前一直用的是JS到处,到时远远没法满足自己的需求,所以入手了PHPExcel,刚开始头大了,什么跟什么呀,多亏了度娘和万能的网友,成功做出了,这里就简单的复述一下步骤。
首先现在PHPExcel下载:http://www.php.cn/xiazai/leiku/1491。里面有用的部分CLASS。
然后,将其打包放在项目的vendor这个目录下。
然后,在想导出excel的php中填上:
vendor("PHPExcel.PHPExcel.PHPExcel");
vendor("PHPExcel");
最后常规代码根据自己的需求进行更改即可:
$data=array("想要到处的内容");
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Phpmarker")->setLastModifiedBy("Phpmarker")->settitle("Phpmarker")->setSubject("Phpmarker")->setDescription("Phpmarker")->setKeywords("Phpmarker")->setCategory("Phpmarker");
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '下单时间')->setCellValue('B1', '订单号')->setCellValue('C1', '商品')->setCellValue('D1', '数量')->setCellValue('E1', '总价')->setCellValue('F1', '是否支付')->setCellValue('G1', '盒子信息');
// Rename worksheet
$objPHPExcel->getActiveSheet()->settitle('Phpmarker-' . date('Y-m-d'));
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
$objPHPExcel->getActiveSheet()->freezePane('A2');
$i =2;//从第几行开始插入
foreach($message as $data){
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data['addtime'])->getstyle('A'.$i)->getAlignment()->setHorizontal(\PHPExcel_style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data['ordernumber']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data['title']);
$objPHPExcel->getActiveSheet()->setCellValueExplicit('D'. $i, $data['pronum'],\PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle('D' . $i)->getNumberFormat()->setFormatCode("@");
// 设置文本格式
$objPHPExcel->getActiveSheet()->setCellValueExplicit('E'. $i, $data['price'],\PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle('E' . $i)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $data['status']);
$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $data['thishezi']);
// $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $data['sushe']);
// $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $data['name']);
// $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $data['tel']);
// $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $data['price']);
// $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $data['status']);
$i++ ;
}
$filename = '2015030423';
ob_end_clean();//清除缓冲区,避免乱码
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');