項(xiàng)目中遇到了一個(gè)到 處,之前一直用的是JS到處九秀,到時(shí)遠(yuǎn)遠(yuǎn)沒(méi)法滿(mǎn)足自己的需求遗嗽,所以入手了PHPExcel,剛開(kāi)始頭大了鼓蜒,什么跟什么呀痹换,多虧了度娘和萬(wàn)能的網(wǎng)友,成功做出了都弹,這里就簡(jiǎn)單的復(fù)述一下步驟晴音。
首先現(xiàn)在PHPExcel下載:http://www.php.cn/xiazai/leiku/1491。里面有用的部分CLASS缔杉。
然后锤躁,將其打包放在項(xiàng)目的vendor這個(gè)目錄下。
然后或详,在想導(dǎo)出excel的php中填上:
vendor("PHPExcel.PHPExcel.PHPExcel");
vendor("PHPExcel");
最后常規(guī)代碼根據(jù)自己的需求進(jìn)行更改即可:
$data=array("想要到處的內(nèi)容");
$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', '下單時(shí)間')->setCellValue('B1', '訂單號(hào)')->setCellValue('C1', '商品')->setCellValue('D1', '數(shù)量')->setCellValue('E1', '總價(jià)')->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;//從第幾行開(kāi)始插入
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("@");
// 設(shè)置文本格式
$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();//清除緩沖區(qū),避免亂碼
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');