导出数据为excel表格

时间:2022-12-25 09:31:17
http://blog.csdn.net/lampsunny/article/details/8992502
我用的是这个,但这个在linux下不能使用,
并且在WINDOWS下导出后,excel表,前三行是多出来了

3 个解决方案

#1


用phpexcel吧

#2


PHPExcel

#3


function PHPExcel($data,$name='index',$title='index'){
$objPHPExcel=Yaf_Registry::get('Phpexcel');
$objPHPExcel = new PHPExcel();
//A-?-Z ASCII码
$columns=65+count($data[0]);
foreach($data as $k => $v){
$num=$k+2;
foreach ($v as $key => $val){
$list[]=$val;
}
for($i=65;$i<=$columns;$i++){
if($i>90){
$iasc=$i-26;
$asc='A'.chr($iasc);
}else{
$asc=chr($i);
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($asc.$num, $list[$i-65]);
}
unset($list);
}
//生成报表标题栏
foreach ($v as $ks =>$vs){
$field[]=$ks;
}
for ($i=65;$i<=$columns;$i++){
if($i>90){
$iasc=$i-26;
$asc='A'.chr($iasc);
}else{
$asc=chr($i);
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($asc.'1', $field[$i-65]);
}
$objPHPExcel->getActiveSheet()->setTitle($title);
$objPHPExcel->setActiveSheetIndex(0);
//只提供下载,不保存服务器
if($title=='cmb' || $title=='供应商列表' || $title=='用户钱包' || $title=='同城结算'){
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name);
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
             exit;
}else {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(APP_PATH.'/uploads/excel/'.$name);
}
}

#1


用phpexcel吧

#2


PHPExcel

#3


function PHPExcel($data,$name='index',$title='index'){
$objPHPExcel=Yaf_Registry::get('Phpexcel');
$objPHPExcel = new PHPExcel();
//A-?-Z ASCII码
$columns=65+count($data[0]);
foreach($data as $k => $v){
$num=$k+2;
foreach ($v as $key => $val){
$list[]=$val;
}
for($i=65;$i<=$columns;$i++){
if($i>90){
$iasc=$i-26;
$asc='A'.chr($iasc);
}else{
$asc=chr($i);
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($asc.$num, $list[$i-65]);
}
unset($list);
}
//生成报表标题栏
foreach ($v as $ks =>$vs){
$field[]=$ks;
}
for ($i=65;$i<=$columns;$i++){
if($i>90){
$iasc=$i-26;
$asc='A'.chr($iasc);
}else{
$asc=chr($i);
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($asc.'1', $field[$i-65]);
}
$objPHPExcel->getActiveSheet()->setTitle($title);
$objPHPExcel->setActiveSheetIndex(0);
//只提供下载,不保存服务器
if($title=='cmb' || $title=='供应商列表' || $title=='用户钱包' || $title=='同城结算'){
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name);
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
             exit;
}else {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(APP_PATH.'/uploads/excel/'.$name);
}
}