导出方法一 使用table输出导出
public function export(){
set_time_limit(0);
ini_set('memory_limit','1024M');
$ranking = self::getResult();
$_pre = array(
"listing" => "ListingID",
"salesbefore" => "前十天销量",
"salesafter" => "后十天销量",
"saleschange" => "涨跌销量",
"percentchange" => "涨跌幅度(%)",
);
$date = date("YmdHis");
$content = getXLSFromList($_pre,$ranking);
header("Content-type:application/vnd.ms-execl;charset=gb2312");
header("Content-Disposition:attactment;filename=销量变化排行榜".$date.".xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $content;
exit;
}
function getXLSFromList($pres,$lists){
$keys=array_keys($pres);
$content="";
$content.="<table border='1'><tr>";
foreach($pres as $_pre){
$content.="<td>$_pre</td>";
}
$content.="</tr>";
foreach($lists as $_list){
$content.= "<tr>";
foreach($keys as $key){
$content.= "<td style='vnd.ms-excel.numberformat:@'>".$_list[$key]."</td>";
}
$content.="</tr>";
}
$content.="</table>";
return $content;
}
导出方法二 使用PHPExcel导出
public function export(){
set_time_limit(0);
ini_set('memory_limit','1024M');
$ranking = self::getResult();
include_once(EXTEND_PATH . 'Library/ORG/PHPExcel/PHPExcel.class.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("ctos")
->setLastModifiedBy("ctos")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Listing ID')
->setCellValue('B1', '前十天销量')
->setCellValue('C1', '后十天销量')
->setCellValue('D1', '涨跌销量')
->setCellValue('E1', '涨跌幅度(%)')
$a = 1;
foreach($ranking as $k){
$a++;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueExplicit('A'.$a, $k['listing'],PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValue('B'.$a, $k['salesbefore'])
->setCellValue('C'.$a, $k['salesafter'])
->setCellValue('D'.$a, $k['saleschange'])
->setCellValue('E'.$a, $k['percentchange'])
$objPHPExcel->getActiveSheet()->getStyle("A$a")->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle("B$a")->getAlignment()->setWrapText(true);
}
$objPHPExcel->getActiveSheet()->setTitle('销量变化排行榜');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="销量变化排行榜('.date('YmdHis').').xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}```